home *** CD-ROM | disk | FTP | other *** search
/ Graphics Plus / Graphics Plus.iso / msdos / compresn / dvpeg / src1b / jquant2.asm < prev    next >
Encoding:
Assembly Source File  |  1992-06-09  |  80.6 KB  |  4,015 lines

  1.     .286p
  2.     ifndef    ??version
  3. publicdll macro    name
  4.     public    name
  5.     endm
  6. $comm    macro    name,dist,size,count
  7.     comm    dist name:BYTE:count*size
  8.     endm
  9.     else
  10. $comm    macro    name,dist,size,count
  11.     comm    dist name[size]:BYTE:count
  12.     endm
  13.     endif
  14. _TEXT    segment byte public 'CODE'
  15. _TEXT    ends
  16. DGROUP    group    _DATA,_BSS
  17.     assume    cs:_TEXT,ds:DGROUP
  18. _DATA    segment word public 'DATA'
  19. d@    label    byte
  20. d@w    label    word
  21. _DATA    ends
  22. _BSS    segment word public 'BSS'
  23. b@    label    byte
  24. b@w    label    word
  25. _BSS    ends
  26. _TEXT    segment byte public 'CODE'
  27.     ;
  28.     ;    METHODDEF void
  29.    ;    
  30.     assume    cs:_TEXT
  31. color_quant_prescan    proc    near
  32.     push    bp
  33.     mov    bp,sp
  34.     sub    sp,26
  35.     push    si
  36.     push    di
  37.    ;    
  38.    ;    color_quant_prescan (decompress_info_ptr cinfo, int num_rows,
  39.    ;                 JSAMPIMAGE image_data, JSAMPARRAY workspace)
  40.    ;    {
  41.    ;      register JSAMPROW ptr0, ptr1, ptr2;
  42.    ;      register histptr histp;
  43.    ;      register int c0, c1, c2;
  44.    ;      int row;
  45.     ;      long col;
  46.    ;    
  47.     mov    bx,word ptr [bp+4]
  48.     mov    ax,word ptr [bx+38]
  49.     mov    dx,word ptr [bx+36]
  50.     mov    word ptr [bp-24],ax
  51.     mov    word ptr [bp-26],dx
  52.    ;    
  53.    ;      long width = cinfo->image_width;
  54.    ;    
  55.    ;    
  56.     mov    word ptr [bp-18],0
  57.     jmp    @1@242
  58. @1@50:
  59.    ;    
  60.    ;      for (row = 0; row < num_rows; row++) {
  61.    ;    
  62.     mov    ax,word ptr [bp-18]
  63.     shl    ax,2
  64.     mov    bx,word ptr [bp+8]
  65.     mov    bx,word ptr [bx]
  66.     add    bx,ax
  67.     mov    ax,word ptr [bx+2]
  68.     mov    dx,word ptr [bx]
  69.     mov    word ptr [bp-2],ax
  70.     mov    word ptr [bp-4],dx
  71.    ;    
  72.     ;        ptr0 = image_data[0][row];
  73.    ;    
  74.     mov    ax,word ptr [bp-18]
  75.     shl    ax,2
  76.     mov    bx,word ptr [bp+8]
  77.     mov    bx,word ptr [bx+2]
  78.     add    bx,ax
  79.     mov    ax,word ptr [bx+2]
  80.     mov    dx,word ptr [bx]
  81.     mov    word ptr [bp-6],ax
  82.     mov    word ptr [bp-8],dx
  83.    ;    
  84.    ;        ptr1 = image_data[1][row];
  85.    ;    
  86.     mov    ax,word ptr [bp-18]
  87.     shl    ax,2
  88.     mov    bx,word ptr [bp+8]
  89.     mov    bx,word ptr [bx+4]
  90.     add    bx,ax
  91.     mov    ax,word ptr [bx+2]
  92.     mov    dx,word ptr [bx]
  93.     mov    word ptr [bp-10],ax
  94.     mov    word ptr [bp-12],dx
  95.    ;    
  96.    ;        ptr2 = image_data[2][row];
  97.    ;    
  98.     mov    ax,word ptr [bp-24]
  99.     mov    dx,word ptr [bp-26]
  100.     mov    word ptr [bp-20],ax
  101.     mov    word ptr [bp-22],dx
  102.     jmp    short @1@146
  103. @1@74:
  104.    ;    
  105.    ;        for (col = width; col > 0; col--) {
  106.    ;          /* get pixel value and index into the histogram */
  107.    ;    
  108.     les    bx,dword ptr [bp-4]
  109.     mov    al,byte ptr es:[bx]
  110.     mov    ah,0
  111.     sar    ax,2
  112.     mov    si,ax
  113.     inc    word ptr [bp-4]
  114.     ;
  115.     ;          c0 = GETJSAMPLE(*ptr0++) >> Y_SHIFT;
  116.     ;
  117.     les    bx,dword ptr [bp-8]
  118.     mov    al,byte ptr es:[bx]
  119.     mov    ah,0
  120.     sar    ax,3
  121.     mov    di,ax
  122.     inc    word ptr [bp-8]
  123.    ;    
  124.    ;          c1 = GETJSAMPLE(*ptr1++) >> C_SHIFT;
  125.    ;    
  126.     les    bx,dword ptr [bp-12]
  127.     mov    al,byte ptr es:[bx]
  128.     mov    ah,0
  129.     sar    ax,3
  130.     mov    cx,ax
  131.     inc    word ptr [bp-12]
  132.    ;    
  133.    ;          c2 = GETJSAMPLE(*ptr2++) >> C_SHIFT;
  134.    ;    
  135.     mov    ax,si
  136.     shl    ax,2
  137.     mov    bx,word ptr DGROUP:histogram
  138.     add    bx,ax
  139.     mov    ax,word ptr [bx+2]
  140.     mov    dx,word ptr [bx]
  141.     mov    bx,di
  142.     shl    bx,6
  143.     add    dx,bx
  144.     mov    bx,cx
  145.     shl    bx,1
  146.     add    dx,bx
  147.     mov    word ptr [bp-14],ax
  148.     mov    word ptr [bp-16],dx
  149.    ;    
  150.    ;          histp = & histogram[c0][c1][c2];
  151.    ;          /* increment, check for overflow and undo increment if so. */
  152.    ;          /* We assume unsigned representation here! */
  153.    ;    
  154.     les    bx,dword ptr [bp-16]
  155.     inc    word ptr es:[bx]
  156.     mov    ax,word ptr es:[bx]
  157.     or    ax,ax
  158.     jne    short @1@122
  159.    ;    
  160.    ;          if (++(*histp) == 0)
  161.    ;    
  162.     dec    word ptr es:[bx]
  163. @1@122:
  164.     sub    word ptr [bp-22],1
  165.     sbb    word ptr [bp-20],0
  166. @1@146:
  167.     cmp    word ptr [bp-20],0
  168.     jg    short @1@74
  169.     jne    short @1@218
  170.     cmp    word ptr [bp-22],0
  171.     jbe    @@0
  172.     jmp    @1@74
  173. @@0:
  174. @1@218:
  175.     inc    word ptr [bp-18]
  176. @1@242:
  177.     mov    ax,word ptr [bp-18]
  178.     cmp    ax,word ptr [bp+6]
  179.     jge    @@1
  180.     jmp    @1@50
  181. @@1:
  182.    ;    
  183.    ;        (*histp)--;
  184.    ;        }
  185.    ;      }
  186.    ;    
  187.     pop    di
  188.     pop    si
  189.     leave    
  190.     ret    
  191. color_quant_prescan    endp
  192.    ;    
  193.    ;    LOCAL boxptr
  194.    ;    
  195.     assume    cs:_TEXT
  196. find_biggest_color_pop    proc    near
  197.     push    bp
  198.     mov    bp,sp
  199.     sub    sp,4
  200.     push    si
  201.     push    di
  202.     ;
  203.     ;    find_biggest_color_pop (void)
  204.    ;    /* Find the splittable box with the largest color population */
  205.    ;    /* Returns NULL if no splittable boxes remain */
  206.    ;    {
  207.    ;      register boxptr boxp;
  208.    ;      register int i;
  209.    ;    
  210.     mov    word ptr [bp-2],0
  211.     mov    word ptr [bp-4],0
  212.    ;    
  213.    ;      register long max = 0;
  214.    ;    
  215.     xor    bx,bx
  216.    ;    
  217.    ;      boxptr which = NULL;
  218.    ;      
  219.    ;    
  220.     xor    di,di
  221.     mov    si,word ptr DGROUP:boxlist
  222.     jmp    short @2@242
  223. @2@50:
  224.    ;    
  225.    ;      for (i = 0, boxp = boxlist; i < numboxes; i++, boxp++) {
  226.    ;    
  227.     mov    ax,word ptr [si+14]
  228.     mov    dx,word ptr [si+12]
  229.     cmp    ax,word ptr [bp-2]
  230.     jl    short @2@218
  231.     jg    short @2@122
  232.     cmp    dx,word ptr [bp-4]
  233.     jbe    short @2@218
  234. @2@122:
  235.    ;    
  236.    ;        if (boxp->colorcount > max) {
  237.    ;    
  238.    ;    
  239.    ;          if (boxp->c0max > boxp->c0min || boxp->c1max > boxp->c1min ||
  240.    ;    
  241.     mov    ax,word ptr [si+2]
  242.     cmp    ax,word ptr [si]
  243.     jg    short @2@194
  244.     mov    ax,word ptr [si+6]
  245.     cmp    ax,word ptr [si+4]
  246.     jg    short @2@194
  247.     mov    ax,word ptr [si+10]
  248.     cmp    ax,word ptr [si+8]
  249.     jle    short @2@218
  250. @2@194:
  251.    ;    
  252.    ;          boxp->c2max > boxp->c2min) {
  253.    ;    
  254.     mov    bx,si
  255.    ;    
  256.    ;        which = boxp;
  257.    ;    
  258.     mov    ax,word ptr [si+14]
  259.     mov    dx,word ptr [si+12]
  260.     mov    word ptr [bp-2],ax
  261.     mov    word ptr [bp-4],dx
  262. @2@218:
  263.     inc    di
  264.     add    si,16
  265. @2@242:
  266.     cmp    di,word ptr DGROUP:numboxes
  267.     jl    short @2@50
  268.    ;    
  269.    ;        max = boxp->colorcount;
  270.    ;          }
  271.    ;        }
  272.    ;      }
  273.    ;    
  274.     mov    ax,bx
  275.    ;    
  276.    ;      return which;
  277.    ;    
  278.     pop    di
  279.     pop    si
  280.     leave    
  281.     ret    
  282. find_biggest_color_pop    endp
  283.    ;    
  284.    ;    LOCAL boxptr
  285.    ;    
  286.     assume    cs:_TEXT
  287. find_biggest_volume    proc    near
  288.     push    bp
  289.     mov    bp,sp
  290.     sub    sp,22
  291.     push    si
  292.     push    di
  293.    ;    
  294.    ;    find_biggest_volume (void)
  295.    ;    /* Find the splittable box with the largest (scaled) volume */
  296.    ;    /* Returns NULL if no splittable boxes remain */
  297.    ;    {
  298.    ;      register boxptr boxp;
  299.    ;      register int i;
  300.    ;    
  301.     mov    word ptr [bp-2],0
  302.     mov    word ptr [bp-4],0
  303.    ;    
  304.    ;      register INT32 max = 0;
  305.    ;      register INT32 norm, c0,c1,c2;
  306.    ;    
  307.     mov    word ptr [bp-22],0
  308.    ;    
  309.    ;      boxptr which = NULL;
  310.    ;      
  311.    ;      /* We use 2-norm rather than real volume here.
  312.    ;       * Some care is needed since the differences are expressed in
  313.    ;       * histogram-cell units; if HIST_Y_BITS != HIST_C_BITS, we have to
  314.    ;       * adjust the scaling to get the proper scaled-YCbCr-space distance.
  315.    ;       * This code won't work right if HIST_Y_BITS < HIST_C_BITS,
  316.    ;       * but that shouldn't ever be true.
  317.    ;       * Note norm > 0 iff box is splittable, so need not check separately.
  318.    ;       */
  319.    ;      
  320.    ;    
  321.     xor    di,di
  322.     mov    si,word ptr DGROUP:boxlist
  323.     jmp    @3@170
  324. @3@50:
  325.    ;    
  326.    ;      for (i = 0, boxp = boxlist; i < numboxes; i++, boxp++) {
  327.    ;    
  328.     mov    ax,word ptr [si+2]
  329.     sub    ax,word ptr [si]
  330.     shl    ax,1
  331. .386
  332. movsx    eax, ax
  333. mov    [bp-12], eax
  334. .286
  335.     ;
  336.     ;        c0 = (boxp->c0max - boxp->c0min) * Y_SCALE;
  337.     ;
  338.     mov    ax,word ptr [si+6]
  339.     sub    ax,word ptr [si+4]
  340.     shl    ax,1
  341. .386
  342. movsx    ebx, ax
  343. mov    [bp-16], ebx
  344. .286
  345.     ;
  346.     ;        c1 = (boxp->c1max - boxp->c1min) << (HIST_Y_BITS-HIST_C_BITS);
  347.     ;
  348.     mov    ax,word ptr [si+10]
  349.     sub    ax,word ptr [si+8]
  350.     shl    ax,1
  351. .386
  352. movsx    ecx, ax
  353. mov    [bp-20], ecx
  354.     ;
  355.     ;        c2 = (boxp->c2max - boxp->c2min) << (HIST_Y_BITS-HIST_C_BITS);
  356.     ;
  357. imul    eax, eax
  358. imul    ebx, ebx
  359. imul    ecx, ecx
  360. add    eax, ebx
  361. add    eax, ecx
  362. mov    [bp-8], eax
  363. .286
  364.     ;
  365.     ;        norm = c0*c0 + c1*c1 + c2*c2;
  366.     ;
  367.     mov    ax,word ptr [bp-6]
  368.     mov    dx,word ptr [bp-8]
  369.     cmp    ax,word ptr [bp-2]
  370.     jl    short @3@146
  371.     jg    short @3@122
  372.     cmp    dx,word ptr [bp-4]
  373.     jbe    short @3@146
  374. @3@122:
  375.     ;
  376.     ;        if (norm > max) {
  377.     ;
  378.     mov    word ptr [bp-22],si
  379.     ;
  380.    ;          which = boxp;
  381.    ;    
  382.     mov    ax,word ptr [bp-6]
  383.     mov    dx,word ptr [bp-8]
  384.     mov    word ptr [bp-2],ax
  385.     mov    word ptr [bp-4],dx
  386. @3@146:
  387.     inc    di
  388.     add    si,16
  389. @3@170:
  390.     cmp    di,word ptr DGROUP:numboxes
  391.     jge    @@2
  392.     jmp    @3@50
  393. @@2:
  394.    ;    
  395.    ;          max = norm;
  396.    ;        }
  397.    ;      }
  398.    ;    
  399.     mov    ax,word ptr [bp-22]
  400.    ;    
  401.    ;      return which;
  402.    ;    
  403.     pop    di
  404.     pop    si
  405.     leave    
  406.     ret    
  407. find_biggest_volume    endp
  408.     ;
  409.    ;    LOCAL void
  410.    ;    
  411.     assume    cs:_TEXT
  412. update_box    proc    near
  413.     push    bp
  414.     mov    bp,sp
  415.     sub    sp,20
  416.     push    si
  417.     push    di
  418.    ;    
  419.    ;    update_box (boxptr boxp)
  420.    ;    /* Shrink the min/max bounds of a box to enclose only nonzero elements, */
  421.    ;    /* and recompute its population */
  422.    ;    {
  423.    ;      histptr histp;
  424.    ;      int c0,c1,c2;
  425.    ;      int c0min,c0max,c1min,c1max,c2min,c2max;
  426.    ;      long ccount;
  427.    ;      
  428.    ;    
  429.     mov    bx,word ptr [bp+4]
  430.     mov    ax,word ptr [bx]
  431.     mov    word ptr [bp-6],ax
  432.     mov    ax,word ptr [bx+2]
  433.     mov    word ptr [bp-8],ax
  434.    ;    
  435.    ;      c0min = boxp->c0min;  c0max = boxp->c0max;
  436.    ;    
  437.     mov    ax,word ptr [bx+4]
  438.     mov    word ptr [bp-10],ax
  439.     mov    ax,word ptr [bx+6]
  440.     mov    word ptr [bp-12],ax
  441.    ;    
  442.    ;      c1min = boxp->c1min;  c1max = boxp->c1max;
  443.    ;    
  444.     mov    ax,word ptr [bx+8]
  445.     mov    word ptr [bp-14],ax
  446.     mov    ax,word ptr [bx+10]
  447.     mov    word ptr [bp-16],ax
  448.    ;    
  449.    ;      c2min = boxp->c2min;  c2max = boxp->c2max;
  450.    ;      
  451.    ;    
  452.     mov    ax,word ptr [bp-8]
  453.     cmp    ax,word ptr [bp-6]
  454.     jle    short @4@314
  455.    ;    
  456.    ;      if (c0max > c0min)
  457.    ;    
  458.     mov    ax,word ptr [bp-6]
  459.     mov    cx,ax
  460.     jmp    short @4@290
  461. @4@74:
  462.    ;    
  463.    ;        for (c0 = c0min; c0 <= c0max; c0++)
  464.    ;    
  465.     mov    di,word ptr [bp-10]
  466.     jmp    short @4@242
  467. @4@98:
  468.    ;    
  469.    ;          for (c1 = c1min; c1 <= c1max; c1++) {
  470.    ;    
  471.     mov    ax,cx
  472.     shl    ax,2
  473.     mov    bx,word ptr DGROUP:histogram
  474.     add    bx,ax
  475.     mov    ax,word ptr [bx+2]
  476.     mov    dx,word ptr [bx]
  477.     mov    bx,di
  478.     shl    bx,6
  479.     add    dx,bx
  480.     mov    bx,word ptr [bp-14]
  481.     shl    bx,1
  482.     add    dx,bx
  483.     mov    word ptr [bp-2],ax
  484.     mov    word ptr [bp-4],dx
  485.    ;    
  486.    ;        histp = & histogram[c0][c1][c2min];
  487.    ;    
  488.     mov    si,word ptr [bp-14]
  489.     jmp    short @4@194
  490. @4@122:
  491.     ;
  492.    ;        for (c2 = c2min; c2 <= c2max; c2++)
  493.    ;    
  494.     les    bx,dword ptr [bp-4]
  495.     add    word ptr [bp-4],2
  496.     cmp    word ptr es:[bx],0
  497.     je    short @4@170
  498.    ;    
  499.    ;          if (*histp++ != 0) {
  500.    ;    
  501.     mov    ax,cx
  502.     mov    word ptr [bp-6],ax
  503.     mov    bx,word ptr [bp+4]
  504.     mov    word ptr [bx],ax
  505.    ;    
  506.    ;            boxp->c0min = c0min = c0;
  507.    ;    
  508.     jmp    short @4@314
  509. @4@170:
  510.     inc    si
  511. @4@194:
  512.     cmp    si,word ptr [bp-16]
  513.     jle    short @4@122
  514.     inc    di
  515. @4@242:
  516.     cmp    di,word ptr [bp-12]
  517.     jle    short @4@98
  518.     inc    cx
  519. @4@290:
  520.     mov    ax,cx
  521.     cmp    ax,word ptr [bp-8]
  522.     jle    short @4@74
  523. @4@314:
  524.    ;    
  525.    ;            goto have_c0min;
  526.    ;          }
  527.    ;          }
  528.    ;     have_c0min:
  529.    ;    
  530.     mov    ax,word ptr [bp-8]
  531.     cmp    ax,word ptr [bp-6]
  532.     jle    short @4@602
  533.    ;    
  534.    ;      if (c0max > c0min)
  535.     ;
  536.     mov    cx,ax
  537.     jmp    short @4@578
  538. @4@362:
  539.    ;    
  540.     ;        for (c0 = c0max; c0 >= c0min; c0--)
  541.    ;    
  542.     mov    di,word ptr [bp-10]
  543.     jmp    short @4@530
  544. @4@386:
  545.    ;    
  546.    ;          for (c1 = c1min; c1 <= c1max; c1++) {
  547.    ;    
  548.     mov    ax,cx
  549.     shl    ax,2
  550.     mov    bx,word ptr DGROUP:histogram
  551.     add    bx,ax
  552.     mov    ax,word ptr [bx+2]
  553.     mov    dx,word ptr [bx]
  554.     mov    bx,di
  555.     shl    bx,6
  556.     add    dx,bx
  557.     mov    bx,word ptr [bp-14]
  558.     shl    bx,1
  559.     add    dx,bx
  560.     mov    word ptr [bp-2],ax
  561.     mov    word ptr [bp-4],dx
  562.    ;    
  563.    ;        histp = & histogram[c0][c1][c2min];
  564.    ;    
  565.     mov    si,word ptr [bp-14]
  566.     jmp    short @4@482
  567. @4@410:
  568.    ;    
  569.    ;        for (c2 = c2min; c2 <= c2max; c2++)
  570.    ;    
  571.     les    bx,dword ptr [bp-4]
  572.     add    word ptr [bp-4],2
  573.     cmp    word ptr es:[bx],0
  574.     je    short @4@458
  575.    ;    
  576.    ;          if (*histp++ != 0) {
  577.    ;    
  578.     mov    ax,cx
  579.     mov    word ptr [bp-8],ax
  580.     mov    bx,word ptr [bp+4]
  581.     mov    word ptr [bx+2],ax
  582.    ;    
  583.    ;            boxp->c0max = c0max = c0;
  584.     ;
  585.     jmp    short @4@602
  586. @4@458:
  587.     inc    si
  588. @4@482:
  589.     cmp    si,word ptr [bp-16]
  590.     jle    short @4@410
  591.     inc    di
  592. @4@530:
  593.     cmp    di,word ptr [bp-12]
  594.     jle    short @4@386
  595.     dec    cx
  596. @4@578:
  597.     mov    ax,cx
  598.     cmp    ax,word ptr [bp-6]
  599.     jge    short @4@362
  600. @4@602:
  601.    ;    
  602.    ;            goto have_c0max;
  603.    ;          }
  604.    ;          }
  605.    ;     have_c0max:
  606.    ;    
  607.     mov    ax,word ptr [bp-12]
  608.     cmp    ax,word ptr [bp-10]
  609.     jle    short @4@890
  610.    ;    
  611.    ;      if (c1max > c1min)
  612.    ;    
  613.     mov    di,word ptr [bp-10]
  614.     jmp    short @4@866
  615. @4@650:
  616.    ;    
  617.    ;        for (c1 = c1min; c1 <= c1max; c1++)
  618.    ;    
  619.     mov    ax,word ptr [bp-6]
  620.     mov    cx,ax
  621.     jmp    short @4@818
  622. @4@674:
  623.     ;
  624.    ;          for (c0 = c0min; c0 <= c0max; c0++) {
  625.    ;    
  626.     mov    ax,cx
  627.     shl    ax,2
  628.     mov    bx,word ptr DGROUP:histogram
  629.     add    bx,ax
  630.     mov    ax,word ptr [bx+2]
  631.     mov    dx,word ptr [bx]
  632.     mov    bx,di
  633.     shl    bx,6
  634.     add    dx,bx
  635.     mov    bx,word ptr [bp-14]
  636.     shl    bx,1
  637.     add    dx,bx
  638.     mov    word ptr [bp-2],ax
  639.     mov    word ptr [bp-4],dx
  640.    ;    
  641.    ;        histp = & histogram[c0][c1][c2min];
  642.    ;    
  643.     mov    si,word ptr [bp-14]
  644.     jmp    short @4@770
  645. @4@698:
  646.    ;    
  647.    ;        for (c2 = c2min; c2 <= c2max; c2++)
  648.    ;    
  649.     les    bx,dword ptr [bp-4]
  650.     add    word ptr [bp-4],2
  651.     cmp    word ptr es:[bx],0
  652.     je    short @4@746
  653.    ;    
  654.    ;          if (*histp++ != 0) {
  655.    ;    
  656.     mov    ax,di
  657.     mov    word ptr [bp-10],ax
  658.     mov    bx,word ptr [bp+4]
  659.     mov    word ptr [bx+4],ax
  660.    ;    
  661.    ;            boxp->c1min = c1min = c1;
  662.    ;    
  663.     jmp    short @4@890
  664. @4@746:
  665.     inc    si
  666. @4@770:
  667.     cmp    si,word ptr [bp-16]
  668.     jle    short @4@698
  669.     inc    cx
  670. @4@818:
  671.     mov    ax,cx
  672.     cmp    ax,word ptr [bp-8]
  673.     jle    short @4@674
  674.     inc    di
  675. @4@866:
  676.     cmp    di,word ptr [bp-12]
  677.     jle    short @4@650
  678. @4@890:
  679.    ;    
  680.    ;            goto have_c1min;
  681.    ;          }
  682.    ;          }
  683.    ;     have_c1min:
  684.    ;    
  685.     mov    ax,word ptr [bp-12]
  686.     cmp    ax,word ptr [bp-10]
  687.     jle    short @4@1178
  688.    ;    
  689.    ;      if (c1max > c1min)
  690.    ;    
  691.     mov    di,word ptr [bp-12]
  692.     jmp    short @4@1154
  693. @4@938:
  694.    ;    
  695.    ;        for (c1 = c1max; c1 >= c1min; c1--)
  696.    ;    
  697.     mov    ax,word ptr [bp-6]
  698.     mov    cx,ax
  699.     jmp    short @4@1106
  700. @4@962:
  701.    ;    
  702.    ;          for (c0 = c0min; c0 <= c0max; c0++) {
  703.    ;    
  704.     mov    ax,cx
  705.     shl    ax,2
  706.     mov    bx,word ptr DGROUP:histogram
  707.     add    bx,ax
  708.     mov    ax,word ptr [bx+2]
  709.     mov    dx,word ptr [bx]
  710.     mov    bx,di
  711.     shl    bx,6
  712.     add    dx,bx
  713.     mov    bx,word ptr [bp-14]
  714.     shl    bx,1
  715.     add    dx,bx
  716.     mov    word ptr [bp-2],ax
  717.     mov    word ptr [bp-4],dx
  718.    ;    
  719.    ;        histp = & histogram[c0][c1][c2min];
  720.    ;    
  721.     mov    si,word ptr [bp-14]
  722.     jmp    short @4@1058
  723. @4@986:
  724.    ;    
  725.    ;        for (c2 = c2min; c2 <= c2max; c2++)
  726.    ;    
  727.     les    bx,dword ptr [bp-4]
  728.     add    word ptr [bp-4],2
  729.     cmp    word ptr es:[bx],0
  730.     je    short @4@1034
  731.    ;    
  732.    ;          if (*histp++ != 0) {
  733.    ;    
  734.     mov    ax,di
  735.     mov    word ptr [bp-12],ax
  736.     mov    bx,word ptr [bp+4]
  737.     mov    word ptr [bx+6],ax
  738.    ;    
  739.    ;            boxp->c1max = c1max = c1;
  740.    ;    
  741.     jmp    short @4@1178
  742. @4@1034:
  743.     inc    si
  744. @4@1058:
  745.     cmp    si,word ptr [bp-16]
  746.     jle    short @4@986
  747.     inc    cx
  748. @4@1106:
  749.     mov    ax,cx
  750.     cmp    ax,word ptr [bp-8]
  751.     jle    short @4@962
  752.     dec    di
  753. @4@1154:
  754.     cmp    di,word ptr [bp-10]
  755.     jge    short @4@938
  756. @4@1178:
  757.    ;    
  758.    ;            goto have_c1max;
  759.    ;          }
  760.     ;          }
  761.    ;     have_c1max:
  762.    ;    
  763.     mov    ax,word ptr [bp-16]
  764.     cmp    ax,word ptr [bp-14]
  765.     jle    short @4@1466
  766.    ;    
  767.    ;      if (c2max > c2min)
  768.    ;    
  769.     mov    si,word ptr [bp-14]
  770.     jmp    short @4@1442
  771. @4@1226:
  772.    ;    
  773.    ;        for (c2 = c2min; c2 <= c2max; c2++)
  774.    ;    
  775.     mov    ax,word ptr [bp-6]
  776.     mov    cx,ax
  777.     jmp    short @4@1394
  778. @4@1250:
  779.    ;    
  780.    ;          for (c0 = c0min; c0 <= c0max; c0++) {
  781.    ;    
  782.     mov    ax,cx
  783.     shl    ax,2
  784.     mov    bx,word ptr DGROUP:histogram
  785.     add    bx,ax
  786.     mov    ax,word ptr [bx+2]
  787.     mov    dx,word ptr [bx]
  788.     mov    bx,word ptr [bp-10]
  789.     shl    bx,6
  790.     add    dx,bx
  791.     mov    bx,si
  792.     shl    bx,1
  793.     add    dx,bx
  794.     mov    word ptr [bp-2],ax
  795.     mov    word ptr [bp-4],dx
  796.    ;    
  797.    ;        histp = & histogram[c0][c1min][c2];
  798.    ;    
  799.     mov    di,word ptr [bp-10]
  800.     jmp    short @4@1346
  801. @4@1274:
  802.    ;    
  803.    ;        for (c1 = c1min; c1 <= c1max; c1++, histp += HIST_C_ELEMS)
  804.     ;
  805.     les    bx,dword ptr [bp-4]
  806.     cmp    word ptr es:[bx],0
  807.     je    short @4@1322
  808.    ;    
  809.    ;          if (*histp != 0) {
  810.    ;    
  811.     mov    ax,si
  812.     mov    word ptr [bp-14],ax
  813.     mov    bx,word ptr [bp+4]
  814.     mov    word ptr [bx+8],ax
  815.    ;    
  816.    ;            boxp->c2min = c2min = c2;
  817.    ;    
  818.     jmp    short @4@1466
  819. @4@1322:
  820.     inc    di
  821.     add    word ptr [bp-4],64
  822. @4@1346:
  823.     cmp    di,word ptr [bp-12]
  824.     jle    short @4@1274
  825.     inc    cx
  826. @4@1394:
  827.     mov    ax,cx
  828.     cmp    ax,word ptr [bp-8]
  829.     jle    short @4@1250
  830.     inc    si
  831. @4@1442:
  832.     cmp    si,word ptr [bp-16]
  833.     jle    short @4@1226
  834. @4@1466:
  835.    ;    
  836.    ;            goto have_c2min;
  837.    ;          }
  838.    ;          }
  839.    ;     have_c2min:
  840.    ;    
  841.     mov    ax,word ptr [bp-16]
  842.     cmp    ax,word ptr [bp-14]
  843.     jle    short @4@1754
  844.    ;    
  845.    ;      if (c2max > c2min)
  846.    ;    
  847.     mov    si,word ptr [bp-16]
  848.     jmp    short @4@1730
  849. @4@1514:
  850.    ;    
  851.    ;        for (c2 = c2max; c2 >= c2min; c2--)
  852.    ;    
  853.     mov    ax,word ptr [bp-6]
  854.     mov    cx,ax
  855.     jmp    short @4@1682
  856. @4@1538:
  857.    ;    
  858.    ;          for (c0 = c0min; c0 <= c0max; c0++) {
  859.    ;    
  860.     mov    ax,cx
  861.     shl    ax,2
  862.     mov    bx,word ptr DGROUP:histogram
  863.     add    bx,ax
  864.     mov    ax,word ptr [bx+2]
  865.     mov    dx,word ptr [bx]
  866.     mov    bx,word ptr [bp-10]
  867.     shl    bx,6
  868.     add    dx,bx
  869.     mov    bx,si
  870.     shl    bx,1
  871.     add    dx,bx
  872.     mov    word ptr [bp-2],ax
  873.     mov    word ptr [bp-4],dx
  874.    ;    
  875.    ;        histp = & histogram[c0][c1min][c2];
  876.    ;    
  877.     mov    di,word ptr [bp-10]
  878.     jmp    short @4@1634
  879. @4@1562:
  880.    ;    
  881.    ;        for (c1 = c1min; c1 <= c1max; c1++, histp += HIST_C_ELEMS)
  882.    ;    
  883.     les    bx,dword ptr [bp-4]
  884.     cmp    word ptr es:[bx],0
  885.     je    short @4@1610
  886.    ;    
  887.     ;          if (*histp != 0) {
  888.     ;
  889.     mov    ax,si
  890.     mov    word ptr [bp-16],ax
  891.     mov    bx,word ptr [bp+4]
  892.     mov    word ptr [bx+10],ax
  893.    ;    
  894.    ;            boxp->c2max = c2max = c2;
  895.    ;    
  896.     jmp    short @4@1754
  897. @4@1610:
  898.     inc    di
  899.     add    word ptr [bp-4],64
  900. @4@1634:
  901.     cmp    di,word ptr [bp-12]
  902.     jle    short @4@1562
  903.     inc    cx
  904. @4@1682:
  905.     mov    ax,cx
  906.     cmp    ax,word ptr [bp-8]
  907.     jle    short @4@1538
  908.     dec    si
  909. @4@1730:
  910.     cmp    si,word ptr [bp-14]
  911.     jge    short @4@1514
  912. @4@1754:
  913.    ;    
  914.    ;            goto have_c2max;
  915.    ;          }
  916.    ;          }
  917.    ;     have_c2max:
  918.    ;      
  919.    ;      /* Now scan remaining volume of box and compute population */
  920.    ;    
  921.     mov    word ptr [bp-18],0
  922.     mov    word ptr [bp-20],0
  923.    ;    
  924.    ;      ccount = 0;
  925.    ;    
  926.     mov    ax,word ptr [bp-6]
  927.     mov    cx,ax
  928.     jmp    short @4@1994
  929. @4@1778:
  930.    ;    
  931.    ;      for (c0 = c0min; c0 <= c0max; c0++)
  932.     ;
  933.     mov    di,word ptr [bp-10]
  934.     jmp    short @4@1946
  935. @4@1802:
  936.     ;
  937.    ;        for (c1 = c1min; c1 <= c1max; c1++) {
  938.    ;    
  939.     mov    ax,cx
  940.     shl    ax,2
  941.     mov    bx,word ptr DGROUP:histogram
  942.     add    bx,ax
  943.     mov    ax,word ptr [bx+2]
  944.     mov    dx,word ptr [bx]
  945.     mov    bx,di
  946.     shl    bx,6
  947.     add    dx,bx
  948.     mov    bx,word ptr [bp-14]
  949.     shl    bx,1
  950.     add    dx,bx
  951.     mov    word ptr [bp-2],ax
  952.     mov    word ptr [bp-4],dx
  953.    ;    
  954.    ;          histp = & histogram[c0][c1][c2min];
  955.    ;    
  956.     mov    si,word ptr [bp-14]
  957.     jmp    short @4@1898
  958. @4@1826:
  959.    ;    
  960.    ;          for (c2 = c2min; c2 <= c2max; c2++, histp++)
  961.    ;    
  962.     les    bx,dword ptr [bp-4]
  963.     cmp    word ptr es:[bx],0
  964.     je    short @4@1874
  965.    ;    
  966.    ;        if (*histp != 0) {
  967.    ;    
  968.     add    word ptr [bp-20],1
  969.     adc    word ptr [bp-18],0
  970. @4@1874:
  971.     inc    si
  972.     add    word ptr [bp-4],2
  973. @4@1898:
  974.     cmp    si,word ptr [bp-16]
  975.     jle    short @4@1826
  976.     inc    di
  977. @4@1946:
  978.     cmp    di,word ptr [bp-12]
  979.     jle    short @4@1802
  980.     inc    cx
  981. @4@1994:
  982.     mov    ax,cx
  983.     cmp    ax,word ptr [bp-8]
  984.     jle    short @4@1778
  985.    ;    
  986.    ;          ccount++;
  987.    ;        }
  988.    ;        }
  989.    ;    
  990.     mov    bx,word ptr [bp+4]
  991.     mov    ax,word ptr [bp-18]
  992.     mov    dx,word ptr [bp-20]
  993.     mov    word ptr [bx+14],ax
  994.     mov    word ptr [bx+12],dx
  995.    ;    
  996.    ;      boxp->colorcount = ccount;
  997.    ;    
  998.     pop    di
  999.     pop    si
  1000.     leave    
  1001.     ret    
  1002. update_box    endp
  1003.    ;    
  1004.    ;    LOCAL void
  1005.    ;    
  1006.     assume    cs:_TEXT
  1007. median_cut    proc    near
  1008.     push    bp
  1009.     mov    bp,sp
  1010.     sub    sp,12
  1011.     push    si
  1012.     push    di
  1013.     jmp    @5@482
  1014. @5@50:
  1015.    ;    
  1016.    ;    median_cut (int desired_colors)
  1017.    ;    /* Repeatedly select and split the largest box until we have enough boxes */
  1018.    ;    {
  1019.    ;      int n,lb;
  1020.     ;      int c0,c1,c2,cmax;
  1021.    ;      register boxptr b1,b2;
  1022.    ;    
  1023.    ;      while (numboxes < desired_colors) {
  1024.     ;        /* Select box to split */
  1025.    ;        /* Current algorithm: by population for first half, then by volume */
  1026.    ;    
  1027.     mov    ax,word ptr DGROUP:numboxes
  1028.     shl    ax,1
  1029.     cmp    ax,word ptr [bp+4]
  1030.     jg    short @5@98
  1031.    ;    
  1032.    ;        if (numboxes*2 <= desired_colors) {
  1033.    ;    
  1034.     call    near ptr find_biggest_color_pop
  1035.     jmp    short @5@122
  1036. @5@98:
  1037.    ;    
  1038.    ;          b1 = find_biggest_color_pop();
  1039.    ;        } else {
  1040.    ;    
  1041.     call    near ptr find_biggest_volume
  1042. @5@122:
  1043.     mov    si,ax
  1044.    ;    
  1045.    ;          b1 = find_biggest_volume();
  1046.    ;        }
  1047.    ;    
  1048.     or    si,si
  1049.     jne    @@3
  1050.     jmp    @5@506
  1051. @@3:
  1052.    ;    
  1053.    ;        if (b1 == NULL)        /* no splittable boxes left! */
  1054.    ;          break;
  1055.    ;    
  1056.     mov    ax,word ptr DGROUP:numboxes
  1057.     shl    ax,4
  1058.     mov    dx,word ptr DGROUP:boxlist
  1059.     add    dx,ax
  1060.     mov    di,dx
  1061.    ;    
  1062.    ;        b2 = &boxlist[numboxes];    /* where new box will go */
  1063.    ;        /* Copy the color bounds to the new box. */
  1064.     ;
  1065.     mov    ax,word ptr [si+2]
  1066.     mov    word ptr [di+2],ax
  1067.     mov    ax,word ptr [si+6]
  1068.     mov    word ptr [di+6],ax
  1069.     mov    ax,word ptr [si+10]
  1070.     mov    word ptr [di+10],ax
  1071.    ;    
  1072.    ;        b2->c0max = b1->c0max; b2->c1max = b1->c1max; b2->c2max = b1->c2max;
  1073.    ;    
  1074.     mov    ax,word ptr [si]
  1075.     mov    word ptr [di],ax
  1076.     mov    ax,word ptr [si+4]
  1077.     mov    word ptr [di+4],ax
  1078.     mov    ax,word ptr [si+8]
  1079.     mov    word ptr [di+8],ax
  1080.    ;    
  1081.    ;        b2->c0min = b1->c0min; b2->c1min = b1->c1min; b2->c2min = b1->c2min;
  1082.    ;        /* Choose which axis to split the box on.
  1083.    ;         * Current algorithm: longest scaled axis.
  1084.    ;         * See notes in find_biggest_volume about scaling...
  1085.    ;         */
  1086.    ;    
  1087.     mov    ax,word ptr [si+2]
  1088.     sub    ax,word ptr [si]
  1089.     shl    ax,1
  1090.     mov    word ptr [bp-6],ax
  1091.    ;    
  1092.    ;        c0 = (b1->c0max - b1->c0min) * Y_SCALE;
  1093.    ;    
  1094.     mov    ax,word ptr [si+6]
  1095.     sub    ax,word ptr [si+4]
  1096.     shl    ax,1
  1097.     mov    word ptr [bp-8],ax
  1098.    ;    
  1099.    ;        c1 = (b1->c1max - b1->c1min) << (HIST_Y_BITS-HIST_C_BITS);
  1100.    ;    
  1101.     mov    ax,word ptr [si+10]
  1102.     sub    ax,word ptr [si+8]
  1103.     shl    ax,1
  1104.     mov    word ptr [bp-10],ax
  1105.    ;    
  1106.    ;        c2 = (b1->c2max - b1->c2min) << (HIST_Y_BITS-HIST_C_BITS);
  1107.    ;    
  1108.     mov    ax,word ptr [bp-6]
  1109.     mov    word ptr [bp-12],ax
  1110.     mov    word ptr [bp-2],0
  1111.    ;    
  1112.     ;        cmax = c0; n = 0;
  1113.    ;    
  1114.     mov    ax,word ptr [bp-8]
  1115.     cmp    ax,word ptr [bp-12]
  1116.     jle    short @5@218
  1117.     mov    word ptr [bp-12],ax
  1118.     mov    word ptr [bp-2],1
  1119. @5@218:
  1120.    ;    
  1121.    ;        if (c1 > cmax) { cmax = c1; n = 1; }
  1122.    ;    
  1123.     mov    ax,word ptr [bp-10]
  1124.     cmp    ax,word ptr [bp-12]
  1125.     jle    short @5@266
  1126.     mov    word ptr [bp-2],2
  1127. @5@266:
  1128.    ;    
  1129.    ;        if (c2 > cmax) { n = 2; }
  1130.    ;        /* Choose split point along selected axis, and update box bounds.
  1131.    ;         * Current algorithm: split at halfway point.
  1132.    ;         * (Since the box has been shrunk to minimum volume,
  1133.    ;         * any split will produce two nonempty subboxes.)
  1134.    ;         * Note that lb value is max for lower box, so must be < old max.
  1135.    ;         */
  1136.    ;    
  1137.     mov    ax,word ptr [bp-2]
  1138.     or    ax,ax
  1139.     je    short @5@386
  1140.     cmp    ax,1
  1141.     je    short @5@410
  1142.     cmp    ax,2
  1143.     je    short @5@434
  1144.     jmp    short @5@458
  1145. @5@386:
  1146.    ;    
  1147.    ;        switch (n) {
  1148.    ;        case 0:
  1149.    ;    
  1150.     mov    ax,word ptr [si+2]
  1151.     add    ax,word ptr [si]
  1152.     mov    bx,2
  1153.     cwd    
  1154.     idiv    bx
  1155.     mov    word ptr [bp-4],ax
  1156.     ;
  1157.    ;          lb = (b1->c0max + b1->c0min) / 2;
  1158.    ;    
  1159.     mov    word ptr [si+2],ax
  1160.    ;    
  1161.    ;          b1->c0max = lb;
  1162.    ;    
  1163.     inc    ax
  1164.     mov    word ptr [di],ax
  1165.    ;    
  1166.    ;          b2->c0min = lb+1;
  1167.    ;    
  1168.     jmp    short @5@458
  1169. @5@410:
  1170.    ;    
  1171.    ;          break;
  1172.    ;        case 1:
  1173.    ;    
  1174.     mov    ax,word ptr [si+6]
  1175.     add    ax,word ptr [si+4]
  1176.     mov    bx,2
  1177.     cwd    
  1178.     idiv    bx
  1179.     mov    word ptr [bp-4],ax
  1180.    ;    
  1181.    ;          lb = (b1->c1max + b1->c1min) / 2;
  1182.    ;    
  1183.     mov    word ptr [si+6],ax
  1184.    ;    
  1185.    ;          b1->c1max = lb;
  1186.    ;    
  1187.     inc    ax
  1188.     mov    word ptr [di+4],ax
  1189.    ;    
  1190.    ;          b2->c1min = lb+1;
  1191.    ;    
  1192.     jmp    short @5@458
  1193. @5@434:
  1194.    ;    
  1195.    ;          break;
  1196.     ;        case 2:
  1197.    ;    
  1198.     mov    ax,word ptr [si+10]
  1199.     add    ax,word ptr [si+8]
  1200.     mov    bx,2
  1201.     cwd    
  1202.     idiv    bx
  1203.     mov    word ptr [bp-4],ax
  1204.    ;    
  1205.    ;          lb = (b1->c2max + b1->c2min) / 2;
  1206.    ;    
  1207.     mov    word ptr [si+10],ax
  1208.    ;    
  1209.    ;          b1->c2max = lb;
  1210.    ;    
  1211.     inc    ax
  1212.     mov    word ptr [di+8],ax
  1213.    ;    
  1214.    ;          b2->c2min = lb+1;
  1215.    ;    
  1216. @5@458:
  1217.    ;    
  1218.    ;          break;
  1219.    ;        }
  1220.    ;        /* Update stats for boxes */
  1221.    ;    
  1222.     push    si
  1223.     call    near ptr update_box
  1224.     inc    sp
  1225.     inc    sp
  1226.    ;    
  1227.    ;        update_box(b1);
  1228.    ;    
  1229.     push    di
  1230.     call    near ptr update_box
  1231.     inc    sp
  1232.     inc    sp
  1233.    ;    
  1234.    ;        update_box(b2);
  1235.    ;    
  1236.     inc    word ptr DGROUP:numboxes
  1237. @5@482:
  1238.     mov    ax,word ptr DGROUP:numboxes
  1239.     cmp    ax,word ptr [bp+4]
  1240.     jge    @@4
  1241.     jmp    @5@50
  1242. @@4:
  1243. @5@506:
  1244.     ;
  1245.    ;        numboxes++;
  1246.    ;      }
  1247.    ;    
  1248.     pop    di
  1249.     pop    si
  1250.     leave    
  1251.     ret    
  1252. median_cut    endp
  1253.    ;    
  1254.    ;    LOCAL void
  1255.    ;    
  1256.     assume    cs:_TEXT
  1257. compute_color    proc    near
  1258.     push    bp
  1259.     mov    bp,sp
  1260.     sub    sp,38
  1261.     push    si
  1262.     push    di
  1263.    ;    
  1264.    ;    compute_color (boxptr boxp, int icolor)
  1265.    ;    /* Compute representative color for a box, put it in my_colormap[icolor] */
  1266.    ;    {
  1267.    ;      /* Current algorithm: mean weighted by pixels (not colors) */
  1268.    ;      /* Note it is important to get the rounding correct! */
  1269.    ;      histptr histp;
  1270.    ;      int c0,c1,c2;
  1271.    ;      int c0min,c0max,c1min,c1max,c2min,c2max;
  1272.    ;      long count;
  1273.    ;    
  1274.     mov    word ptr [bp-24],0
  1275.     mov    word ptr [bp-26],0
  1276.    ;    
  1277.    ;      long total = 0;
  1278.    ;    
  1279.     mov    word ptr [bp-28],0
  1280.     mov    word ptr [bp-30],0
  1281.    ;    
  1282.    ;      long c0total = 0;
  1283.    ;    
  1284.     mov    word ptr [bp-32],0
  1285.     mov    word ptr [bp-34],0
  1286.     ;
  1287.    ;      long c1total = 0;
  1288.     ;
  1289.     mov    word ptr [bp-36],0
  1290.     mov    word ptr [bp-38],0
  1291.    ;    
  1292.    ;      long c2total = 0;
  1293.    ;      
  1294.    ;    
  1295.     mov    bx,word ptr [bp+4]
  1296.     mov    ax,word ptr [bx]
  1297.     mov    word ptr [bp-8],ax
  1298.     mov    ax,word ptr [bx+2]
  1299.     mov    word ptr [bp-10],ax
  1300.    ;    
  1301.    ;      c0min = boxp->c0min;  c0max = boxp->c0max;
  1302.    ;    
  1303.     mov    ax,word ptr [bx+4]
  1304.     mov    word ptr [bp-12],ax
  1305.     mov    ax,word ptr [bx+6]
  1306.     mov    word ptr [bp-14],ax
  1307.    ;    
  1308.    ;      c1min = boxp->c1min;  c1max = boxp->c1max;
  1309.    ;    
  1310.     mov    ax,word ptr [bx+8]
  1311.     mov    word ptr [bp-16],ax
  1312.     mov    ax,word ptr [bx+10]
  1313.     mov    word ptr [bp-18],ax
  1314.    ;    
  1315.    ;      c2min = boxp->c2min;  c2max = boxp->c2max;
  1316.    ;      
  1317.    ;    
  1318.     mov    ax,word ptr [bp-8]
  1319.     mov    word ptr [bp-6],ax
  1320.     jmp    @6@266
  1321. @6@50:
  1322.    ;    
  1323.    ;      for (c0 = c0min; c0 <= c0max; c0++)
  1324.    ;    
  1325.     mov    di,word ptr [bp-12]
  1326.     jmp    @6@218
  1327. @6@74:
  1328.     ;
  1329.    ;        for (c1 = c1min; c1 <= c1max; c1++) {
  1330.     ;
  1331.     mov    ax,word ptr [bp-6]
  1332.     shl    ax,2
  1333.     mov    bx,word ptr DGROUP:histogram
  1334.     add    bx,ax
  1335.     mov    ax,word ptr [bx+2]
  1336.     mov    dx,word ptr [bx]
  1337.     mov    bx,di
  1338.     shl    bx,6
  1339.     add    dx,bx
  1340.     mov    bx,word ptr [bp-16]
  1341.     shl    bx,1
  1342.     add    dx,bx
  1343.     mov    word ptr [bp-2],ax
  1344.     mov    word ptr [bp-4],dx
  1345.    ;    
  1346.    ;          histp = & histogram[c0][c1][c2min];
  1347.    ;    
  1348.     mov    si,word ptr [bp-16]
  1349.     jmp    short @6@170
  1350. @6@98:
  1351.    ;    
  1352.    ;          for (c2 = c2min; c2 <= c2max; c2++) {
  1353.    ;    
  1354.     les    bx,dword ptr [bp-4]
  1355.     add    word ptr [bp-4],2
  1356.     mov    ax,word ptr es:[bx]
  1357.     xor    dx,dx
  1358.     mov    word ptr [bp-20],dx
  1359.     mov    word ptr [bp-22],ax
  1360.     or    ax,dx
  1361.     je    short @6@146
  1362.     ;
  1363.     ;        if ((count = *histp++) != 0) {
  1364.     ;
  1365. .386
  1366. mov    eax, [bp-22]
  1367. add    [bp-26], eax
  1368.     ;
  1369.     ;          total += count;
  1370.     ;
  1371. movsx edx, word ptr [bp-6]
  1372. shl    edx, 2
  1373. add    edx, 2
  1374. imul    edx, eax
  1375. add    [bp-30], edx
  1376.     ;
  1377.     ;          c0total += ((c0 << Y_SHIFT) + ((1<<Y_SHIFT)>>1)) * count;
  1378.     ;
  1379. movsx    edx, di
  1380. shl    edx, 3
  1381. add    edx, 4
  1382. imul    edx, eax
  1383. add    [bp-34], edx
  1384.     ;
  1385.     ;          c1total += ((c1 << C_SHIFT) + ((1<<C_SHIFT)>>1)) * count;
  1386.     ;
  1387. movsx    edx, si
  1388. shl    edx, 3
  1389. add    edx, 4
  1390. imul    edx, eax
  1391. add    [bp-38], edx
  1392. .286
  1393. @6@146:
  1394.     inc    si
  1395. @6@170:
  1396.     cmp    si,word ptr [bp-18]
  1397.     jle    short @6@98
  1398.     inc    di
  1399. @6@218:
  1400.     cmp    di,word ptr [bp-14]
  1401.     jg    @@5
  1402.     jmp    @6@74
  1403. @@5:
  1404.     inc    word ptr [bp-6]
  1405. @6@266:
  1406.     mov    ax,word ptr [bp-6]
  1407.     cmp    ax,word ptr [bp-10]
  1408.     jg    @@6
  1409.     jmp    @6@50
  1410. @@6:
  1411.     ;
  1412.     ;          c2total += ((c2 << C_SHIFT) + ((1<<C_SHIFT)>>1)) * count;
  1413.     ;        }
  1414.    ;          }
  1415.    ;        }
  1416.    ;      
  1417.    ;    
  1418. ;mov    eax, [bp-26]
  1419. ;mov    edx, eax
  1420. ;sar    edx, 1
  1421.     push    word ptr [bp-24]
  1422.     push    word ptr [bp-26]
  1423.     mov    ax,word ptr [bp-24]
  1424.     mov    dx,word ptr [bp-26]
  1425.     sar    ax,1
  1426.     rcr    dx,1
  1427.     mov    bx,word ptr [bp-28]
  1428.     mov    cx,word ptr [bp-30]
  1429.     add    cx,dx
  1430.     adc    bx,ax
  1431.     push    bx
  1432.     push    cx
  1433.     call    near ptr N_LDIV@
  1434.     mov    bx,word ptr DGROUP:my_colormap
  1435.     les    bx,dword ptr [bx]
  1436.     add    bx,word ptr [bp+6]
  1437.     mov    byte ptr es:[bx],al
  1438.     ;
  1439.     ;      my_colormap[0][icolor] = (JSAMPLE) ((c0total + (total>>1)) / total);
  1440.     ;
  1441.     push    word ptr [bp-24]
  1442.     push    word ptr [bp-26]
  1443.     mov    ax,word ptr [bp-24]
  1444.     mov    dx,word ptr [bp-26]
  1445.     sar    ax,1
  1446.     rcr    dx,1
  1447.     mov    bx,word ptr [bp-32]
  1448.     mov    cx,word ptr [bp-34]
  1449.     add    cx,dx
  1450.     adc    bx,ax
  1451.     push    bx
  1452.     push    cx
  1453.     call    near ptr N_LDIV@
  1454.     mov    bx,word ptr DGROUP:my_colormap
  1455.     les    bx,dword ptr [bx+4]
  1456.     add    bx,word ptr [bp+6]
  1457.     mov    byte ptr es:[bx],al
  1458.     ;
  1459.     ;      my_colormap[1][icolor] = (JSAMPLE) ((c1total + (total>>1)) / total);
  1460.     ;
  1461.     push    word ptr [bp-24]
  1462.     push    word ptr [bp-26]
  1463.     mov    ax,word ptr [bp-24]
  1464.     mov    dx,word ptr [bp-26]
  1465.     sar    ax,1
  1466.     rcr    dx,1
  1467.     mov    bx,word ptr [bp-36]
  1468.     mov    cx,word ptr [bp-38]
  1469.     add    cx,dx
  1470.     adc    bx,ax
  1471.     push    bx
  1472.     push    cx
  1473.     call    near ptr N_LDIV@
  1474.     mov    bx,word ptr DGROUP:my_colormap
  1475.     les    bx,dword ptr [bx+8]
  1476.     add    bx,word ptr [bp+6]
  1477.     mov    byte ptr es:[bx],al
  1478.    ;    
  1479.    ;      my_colormap[2][icolor] = (JSAMPLE) ((c2total + (total>>1)) / total);
  1480.    ;    
  1481.     pop    di
  1482.     pop    si
  1483.     leave    
  1484.     ret
  1485. compute_color    endp
  1486.    ;    
  1487.    ;    LOCAL void
  1488.    ;    
  1489.     assume    cs:_TEXT
  1490. remap_colormap    proc    near
  1491.     push    bp
  1492.     mov    bp,sp
  1493.     sub    sp,26
  1494.     push    si
  1495.     push    di
  1496.     mov    di,word ptr [bp+4]
  1497.    ;    
  1498.    ;    remap_colormap (decompress_info_ptr cinfo)
  1499.    ;    /* Remap the internal colormap to the output colorspace */
  1500.    ;    {
  1501.    ;      /* This requires a little trickery since color_convert expects to
  1502.    ;       * deal with 3-D arrays (a 2-D sample array for each component).
  1503.    ;       * We must promote the colormaps into one-row 3-D arrays.
  1504.    ;       */
  1505.    ;      short ci;
  1506.    ;      JSAMPARRAY input_hack[3];
  1507.    ;      JSAMPARRAY output_hack[10];    /* assume no more than 10 output components */
  1508.    ;    
  1509.    ;    
  1510.     xor    si,si
  1511.     jmp    short @7@98
  1512. @7@50:
  1513.    ;    
  1514.    ;      for (ci = 0; ci < 3; ci++)
  1515.    ;    
  1516.     mov    bx,si
  1517.     shl    bx,1
  1518.     lea    ax,word ptr [bp-6]
  1519.     add    bx,ax
  1520.     mov    ax,si
  1521.     shl    ax,2
  1522.     mov    dx,word ptr DGROUP:my_colormap
  1523.     add    dx,ax
  1524.     mov    word ptr [bx],dx
  1525.     inc    si
  1526. @7@98:
  1527.     cmp    si,3
  1528.     jl    short @7@50
  1529.    ;    
  1530.    ;        input_hack[ci] = &(my_colormap[ci]);
  1531.    ;    
  1532.     xor    si,si
  1533.     jmp    short @7@194
  1534. @7@146:
  1535.    ;    
  1536.    ;      for (ci = 0; ci < cinfo->color_out_comps; ci++)
  1537.    ;    
  1538.     mov    ax,si
  1539.     shl    ax,2
  1540.     mov    dx,word ptr [di+145]
  1541.     add    dx,ax
  1542.     mov    bx,si
  1543.     shl    bx,1
  1544.     lea    ax,word ptr [bp-26]
  1545.     add    bx,ax
  1546.     mov    word ptr [bx],dx
  1547.     inc    si
  1548. @7@194:
  1549.     cmp    word ptr [di+139],si
  1550.     jg    short @7@146
  1551.    ;    
  1552.    ;        output_hack[ci] = &(cinfo->colormap[ci]);
  1553.    ;    
  1554.    ;    
  1555.    ;    
  1556.     ;      (*cinfo->methods->color_convert) (cinfo, 1,
  1557.    ;                        (long) cinfo->actual_number_of_colors,
  1558.    ;    
  1559.     lea    ax,word ptr [bp-26]
  1560.     push    ax
  1561.     lea    ax,word ptr [bp-6]
  1562.     push    ax
  1563.     mov    ax,word ptr [di+143]
  1564.     cwd    
  1565.     push    dx
  1566.     push    ax
  1567.     push    1
  1568.     push    di
  1569.     mov    bx,word ptr [di]
  1570.     call    word ptr [bx+44]
  1571.     add    sp,12
  1572.    ;    
  1573.    ;                        input_hack, output_hack);
  1574.    ;    
  1575.     pop    di
  1576.     pop    si
  1577.     leave    
  1578.     ret    
  1579. remap_colormap    endp
  1580.    ;    
  1581.    ;    LOCAL void
  1582.    ;    
  1583.     assume    cs:_TEXT
  1584. select_colors    proc    near
  1585.     push    bp
  1586.     mov    bp,sp
  1587.     dec    sp
  1588.     dec    sp
  1589.     push    si
  1590.     push    di
  1591.     mov    si,word ptr [bp+4]
  1592.    ;    
  1593.    ;    select_colors (decompress_info_ptr cinfo)
  1594.    ;    /* Master routine for color selection */
  1595.    ;    {
  1596.    ;    
  1597.     mov    ax,word ptr [si+24]
  1598.     mov    word ptr [bp-2],ax
  1599.    ;    
  1600.     ;      int desired = cinfo->desired_number_of_colors;
  1601.    ;      int i;
  1602.    ;    
  1603.    ;      /* Allocate workspace for box list */
  1604.    ;    
  1605.     shl    ax,4
  1606.     push    ax
  1607.     mov    bx,word ptr [si+2]
  1608.     call    word ptr [bx+22]
  1609.     inc    sp
  1610.     inc    sp
  1611.     mov    word ptr DGROUP:boxlist,ax
  1612.    ;    
  1613.    ;      boxlist = (boxptr) (*cinfo->emethods->alloc_small) (desired * SIZEOF(box));
  1614.     ;      /* Initialize one box containing whole space */
  1615.    ;    
  1616.     mov    word ptr DGROUP:numboxes,1
  1617.    ;    
  1618.    ;      numboxes = 1;
  1619.    ;    
  1620.     mov    bx,word ptr DGROUP:boxlist
  1621.     mov    word ptr [bx],0
  1622.    ;    
  1623.    ;      boxlist[0].c0min = 0;
  1624.    ;    
  1625.     mov    bx,word ptr DGROUP:boxlist
  1626.     mov    word ptr [bx+2],63
  1627.    ;    
  1628.    ;      boxlist[0].c0max = MAXJSAMPLE >> Y_SHIFT;
  1629.    ;    
  1630.     mov    bx,word ptr DGROUP:boxlist
  1631.     mov    word ptr [bx+4],0
  1632.    ;    
  1633.    ;      boxlist[0].c1min = 0;
  1634.    ;    
  1635.     mov    bx,word ptr DGROUP:boxlist
  1636.     mov    word ptr [bx+6],31
  1637.    ;    
  1638.    ;      boxlist[0].c1max = MAXJSAMPLE >> C_SHIFT;
  1639.    ;    
  1640.     mov    bx,word ptr DGROUP:boxlist
  1641.     mov    word ptr [bx+8],0
  1642.    ;    
  1643.    ;      boxlist[0].c2min = 0;
  1644.     ;
  1645.     mov    bx,word ptr DGROUP:boxlist
  1646.     mov    word ptr [bx+10],31
  1647.    ;    
  1648.    ;      boxlist[0].c2max = MAXJSAMPLE >> C_SHIFT;
  1649.    ;      /* Shrink it to actually-used volume and set its statistics */
  1650.    ;    
  1651.     push    word ptr DGROUP:boxlist
  1652.     call    near ptr update_box
  1653.     inc    sp
  1654.     inc    sp
  1655.    ;    
  1656.    ;      update_box(& boxlist[0]);
  1657.    ;      /* Perform median-cut to produce final box list */
  1658.     ;
  1659.     push    word ptr [bp-2]
  1660.     call    near ptr median_cut
  1661.     inc    sp
  1662.     inc    sp
  1663.    ;    
  1664.    ;      median_cut(desired);
  1665.    ;      /* Compute the representative color for each box, fill my_colormap[] */
  1666.    ;    
  1667.     xor    di,di
  1668.     jmp    short @8@98
  1669. @8@50:
  1670.    ;    
  1671.    ;      for (i = 0; i < numboxes; i++)
  1672.    ;    
  1673.     push    di
  1674.     mov    ax,di
  1675.     shl    ax,4
  1676.     mov    dx,word ptr DGROUP:boxlist
  1677.     add    dx,ax
  1678.     push    dx
  1679.     call    near ptr compute_color
  1680.     add    sp,4
  1681.     inc    di
  1682. @8@98:
  1683.     cmp    di,word ptr DGROUP:numboxes
  1684.     jl    short @8@50
  1685.    ;    
  1686.    ;        compute_color(& boxlist[i], i);
  1687.    ;    
  1688.     mov    ax,word ptr DGROUP:numboxes
  1689.     mov    word ptr [si+143],ax
  1690.    ;    
  1691.    ;      cinfo->actual_number_of_colors = numboxes;
  1692.    ;      /* Produce an output colormap in the desired output colorspace */
  1693.    ;    
  1694.     push    si
  1695.     call    near ptr remap_colormap
  1696.     inc    sp
  1697.     inc    sp
  1698.    ;    
  1699.    ;      remap_colormap(cinfo);
  1700.    ;      TRACEMS1(cinfo->emethods, 1, "Selected %d colors for quantization",
  1701.    ;    
  1702.     mov    bx,word ptr [si+2]
  1703.     cmp    word ptr [bx+4],1
  1704.     jl    short @8@194
  1705.     mov    bx,word ptr [si+2]
  1706.     mov    ax,word ptr DGROUP:numboxes
  1707.     mov    word ptr [bx+6],ax
  1708.     push    offset DGROUP:s@
  1709.     mov    bx,word ptr [si+2]
  1710.     call    word ptr [bx+2]
  1711.     inc    sp
  1712.     inc    sp
  1713. @8@194:
  1714.    ;    
  1715.    ;           numboxes);
  1716.    ;      /* Done with the box list */
  1717.    ;    
  1718.     push    word ptr DGROUP:boxlist
  1719.     mov    bx,word ptr [si+2]
  1720.     call    word ptr [bx+24]
  1721.     inc    sp
  1722.     inc    sp
  1723.    ;    
  1724.    ;      (*cinfo->emethods->free_small) ((void *) boxlist);
  1725.    ;    
  1726.     pop    di
  1727.     pop    si
  1728.     leave    
  1729.     ret    
  1730. select_colors    endp
  1731.    ;    
  1732.     ;    LOCAL int
  1733.    ;    
  1734.     assume    cs:_TEXT
  1735. find_nearby_colors    proc    near
  1736.     push    bp
  1737.     mov    bp,sp
  1738.     sub    sp,1058
  1739.     push    si
  1740.     push    di
  1741.     mov    di,word ptr [bp+6]
  1742.    ;    
  1743.    ;    find_nearby_colors (decompress_info_ptr cinfo, int minc0, int minc1, int minc2,
  1744.    ;                JSAMPLE colorlist[])
  1745.    ;    /* Locate the colormap entries close enough to an update box to be candidates
  1746.     ;     * for the nearest entry to some cell(s) in the update box.  The update box
  1747.    ;     * is specified by the center coordinates of its first cell.  The number of
  1748.    ;     * candidate colormap entries is returned, and their colormap indexes are
  1749.    ;     * placed in colorlist[].
  1750.    ;     * This routine uses Heckbert's "locally sorted search" criterion to select
  1751.    ;     * the colors that need further consideration.
  1752.    ;     */
  1753.    ;    {
  1754.    ;    
  1755.     mov    bx,word ptr [bp+4]
  1756.     mov    ax,word ptr [bx+143]
  1757.     mov    word ptr [bp-2],ax
  1758.    ;    
  1759.    ;      int numcolors = cinfo->actual_number_of_colors;
  1760.    ;      int maxc0, maxc1, maxc2;
  1761.    ;      int centerc0, centerc1, centerc2;
  1762.    ;      int i, x, ncolors;
  1763.    ;      INT32 minmaxdist, min_dist, max_dist, tdist;
  1764.    ;      INT32 mindist[MAXNUMCOLORS];    /* min distance to colormap entry i */
  1765.    ;    
  1766.    ;      /* Compute true coordinates of update box's upper corner and center.
  1767.    ;       * Actually we compute the coordinates of the center of the upper-corner
  1768.    ;       * histogram cell, which are the upper bounds of the volume we care about.
  1769.    ;       * Note that since ">>" rounds down, the "center" values may be closer to
  1770.    ;       * min than to max; hence comparisons to them must be "<=", not "<".
  1771.    ;       */
  1772.    ;    
  1773.     mov    ax,di
  1774.     add    ax,28
  1775.     mov    word ptr [bp-4],ax
  1776.     ;
  1777.    ;      maxc0 = minc0 + ((1 << BOX_Y_SHIFT) - (1 << Y_SHIFT));
  1778.    ;    
  1779.     mov    ax,di
  1780.     add    ax,word ptr [bp-4]
  1781.     sar    ax,1
  1782.     mov    word ptr [bp-10],ax
  1783.    ;    
  1784.    ;      centerc0 = (minc0 + maxc0) >> 1;
  1785.    ;    
  1786.     mov    ax,word ptr [bp+8]
  1787.     add    ax,24
  1788.     mov    word ptr [bp-6],ax
  1789.    ;    
  1790.     ;      maxc1 = minc1 + ((1 << BOX_C_SHIFT) - (1 << C_SHIFT));
  1791.    ;    
  1792.     mov    ax,word ptr [bp+8]
  1793.     add    ax,word ptr [bp-6]
  1794.     sar    ax,1
  1795.     mov    word ptr [bp-12],ax
  1796.    ;    
  1797.    ;      centerc1 = (minc1 + maxc1) >> 1;
  1798.    ;    
  1799.     mov    ax,word ptr [bp+10]
  1800.     add    ax,24
  1801.     mov    word ptr [bp-8],ax
  1802.    ;    
  1803.    ;      maxc2 = minc2 + ((1 << BOX_C_SHIFT) - (1 << C_SHIFT));
  1804.    ;    
  1805.     mov    ax,word ptr [bp+10]
  1806.     add    ax,word ptr [bp-8]
  1807.     sar    ax,1
  1808.     mov    word ptr [bp-14],ax
  1809.    ;    
  1810.    ;      centerc2 = (minc2 + maxc2) >> 1;
  1811.    ;    
  1812.    ;      /* For each color in colormap, find:
  1813.    ;       *  1. its minimum squared-distance to any point in the update box
  1814.    ;       *     (zero if color is within update box);
  1815.    ;       *  2. its maximum squared-distance to any point in the update box.
  1816.    ;       * Both of these can be found by considering only the corners of the box.
  1817.    ;       * We save the minimum distance for each color in mindist[];
  1818.    ;       * only the smallest maximum distance is of interest.
  1819.    ;       * Note we have to scale Y to get correct distance in scaled space.
  1820.     ;       */
  1821.    ;    
  1822.     mov    word ptr [bp-20],32767
  1823.     mov    word ptr [bp-22],65535
  1824.    ;    
  1825.    ;      minmaxdist = 0x7FFFFFFFL;
  1826.    ;    
  1827.    ;    
  1828.     mov    word ptr [bp-16],0
  1829.     jmp    @9@746
  1830. @9@50:
  1831.    ;    
  1832.    ;      for (i = 0; i < numcolors; i++) {
  1833.    ;        /* We compute the squared-c0-distance term, then add in the other two. */
  1834.     ;
  1835.     mov    bx,word ptr DGROUP:my_colormap
  1836.     les    bx,dword ptr [bx]
  1837.     add    bx,word ptr [bp-16]
  1838.     mov    al,byte ptr es:[bx]
  1839.     mov    ah,0
  1840.     mov    si,ax
  1841.    ;    
  1842.    ;        x = GETJSAMPLE(my_colormap[0][i]);
  1843.    ;    
  1844.     cmp    si,di
  1845.     jge    short @9@98
  1846.    ;    
  1847.    ;        if (x < minc0) {
  1848.    ;    
  1849.     sub    ax,di
  1850.     shl    ax,1
  1851.     cwd    
  1852.     mov    word ptr [bp-32],dx
  1853.     mov    word ptr [bp-34],ax
  1854.    ;    
  1855.    ;          tdist = (x - minc0) * Y_SCALE;
  1856.    ;    
  1857.     mov    cx,word ptr [bp-32]
  1858.     mov    bx,word ptr [bp-34]
  1859.     mov    dx,word ptr [bp-32]
  1860.     call    near ptr N_LXMUL@
  1861.     mov    word ptr [bp-24],dx
  1862.     mov    word ptr [bp-26],ax
  1863.    ;    
  1864.     ;          min_dist = tdist*tdist;
  1865.    ;    
  1866.     mov    ax,si
  1867.     sub    ax,word ptr [bp-4]
  1868.     jmp    short @9@218
  1869. @9@98:
  1870.    ;    
  1871.    ;          tdist = (x - maxc0) * Y_SCALE;
  1872.    ;          max_dist = tdist*tdist;
  1873.    ;    
  1874.     cmp    si,word ptr [bp-4]
  1875.     jle    short @9@146
  1876.    ;    
  1877.    ;        } else if (x > maxc0) {
  1878.     ;
  1879.     mov    ax,si
  1880.     sub    ax,word ptr [bp-4]
  1881.     shl    ax,1
  1882.     cwd    
  1883.     mov    word ptr [bp-32],dx
  1884.     mov    word ptr [bp-34],ax
  1885.    ;    
  1886.    ;          tdist = (x - maxc0) * Y_SCALE;
  1887.    ;    
  1888.     mov    cx,word ptr [bp-32]
  1889.     mov    bx,word ptr [bp-34]
  1890.     mov    dx,word ptr [bp-32]
  1891.     call    near ptr N_LXMUL@
  1892.     mov    word ptr [bp-24],dx
  1893.     mov    word ptr [bp-26],ax
  1894.     jmp    short @9@194
  1895. @9@146:
  1896.    ;    
  1897.    ;          min_dist = tdist*tdist;
  1898.    ;          tdist = (x - minc0) * Y_SCALE;
  1899.    ;          max_dist = tdist*tdist;
  1900.    ;        } else {
  1901.    ;          /* within cell range so no contribution to min_dist */
  1902.    ;    
  1903.     mov    word ptr [bp-24],0
  1904.     mov    word ptr [bp-26],0
  1905.    ;    
  1906.    ;          min_dist = 0;
  1907.    ;    
  1908.     cmp    si,word ptr [bp-10]
  1909.     jg    short @9@194
  1910.    ;    
  1911.    ;          if (x <= centerc0) {
  1912.    ;    
  1913.     mov    ax,si
  1914.     sub    ax,word ptr [bp-4]
  1915.     jmp    short @9@218
  1916. @9@194:
  1917.     ;
  1918.     ;        tdist = (x - maxc0) * Y_SCALE;
  1919.     ;        max_dist = tdist*tdist;
  1920.     ;          } else {
  1921.     ;
  1922.     mov    ax,si
  1923.     sub    ax,di
  1924. @9@218:
  1925.     shl    ax,1
  1926. .386
  1927. movsx    edx, ax
  1928. mov    [bp-34], edx
  1929.     ;
  1930.     ;        tdist = (x - minc0) * Y_SCALE;
  1931.     ;
  1932. imul    edx, edx
  1933. mov    [bp-30], edx
  1934. .286
  1935.     ;
  1936.     ;        max_dist = tdist*tdist;
  1937.     ;          }
  1938.     ;        }
  1939.     ;
  1940.     ;
  1941.     mov    bx,word ptr DGROUP:my_colormap
  1942.     les    bx,dword ptr [bx+4]
  1943.     add    bx,word ptr [bp-16]
  1944.     mov    al,byte ptr es:[bx]
  1945.     mov    ah,0
  1946.     mov    si,ax
  1947.     ;
  1948.     ;        x = GETJSAMPLE(my_colormap[1][i]);
  1949.     ;
  1950.     cmp    si,word ptr [bp+8]
  1951.     jge    short @9@290
  1952.     ;
  1953.     ;        if (x < minc1) {
  1954.     ;
  1955.     sub    ax,word ptr [bp+8]
  1956. .386
  1957. movsx    edx, ax
  1958. mov    [bp-34], edx
  1959.     ;
  1960.     ;          tdist = x - minc1;
  1961.     ;
  1962. imul    edx, edx
  1963. add    [bp-26], edx
  1964. .286
  1965.     ;
  1966.     ;          min_dist += tdist*tdist;
  1967.     ;
  1968.     mov    ax,si
  1969.     sub    ax,word ptr [bp-6]
  1970.     jmp    short @9@410
  1971. @9@290:
  1972.     ;
  1973.     ;          tdist = x - maxc1;
  1974.     ;          max_dist += tdist*tdist;
  1975.     ;
  1976.     cmp    si,word ptr [bp-6]
  1977.     jle    short @9@338
  1978.     ;
  1979.     ;        } else if (x > maxc1) {
  1980.     ;
  1981.     mov    ax,si
  1982.     sub    ax,word ptr [bp-6]
  1983. .386
  1984. movsx    edx, ax
  1985. mov    [bp-34], edx
  1986.     ;
  1987.     ;          tdist = x - maxc1;
  1988.     ;
  1989. imul    edx, edx
  1990. add    [bp-36], edx
  1991. .286
  1992.     jmp    short @9@386
  1993. @9@338:
  1994.    ;    
  1995.    ;          min_dist += tdist*tdist;
  1996.     ;          tdist = x - minc1;
  1997.    ;          max_dist += tdist*tdist;
  1998.    ;        } else {
  1999.    ;          /* within cell range so no contribution to min_dist */
  2000.    ;    
  2001.     cmp    si,word ptr [bp-12]
  2002.     jg    short @9@386
  2003.    ;    
  2004.    ;          if (x <= centerc1) {
  2005.    ;    
  2006.     mov    ax,si
  2007.     sub    ax,word ptr [bp-6]
  2008.     jmp    short @9@410
  2009. @9@386:
  2010.    ;    
  2011.    ;        tdist = x - maxc1;
  2012.    ;        max_dist += tdist*tdist;
  2013.    ;          } else {
  2014.    ;    
  2015.     mov    ax,si
  2016.     sub    ax,word ptr [bp+8]
  2017. @9@410:
  2018. .386
  2019. movsx    edx, ax
  2020. mov    [bp-34], edx
  2021.     ;
  2022.     ;        tdist = x - minc1;
  2023.     ;
  2024. imul    edx, edx
  2025. add    [bp-30], edx
  2026. .286
  2027.     ;
  2028.     ;        max_dist += tdist*tdist;
  2029.     ;          }
  2030.     ;        }
  2031.     ;
  2032.     ;
  2033.     mov    bx,word ptr DGROUP:my_colormap
  2034.     les    bx,dword ptr [bx+8]
  2035.     add    bx,word ptr [bp-16]
  2036.     mov    al,byte ptr es:[bx]
  2037.     mov    ah,0
  2038.     mov    si,ax
  2039.     ;
  2040.     ;        x = GETJSAMPLE(my_colormap[2][i]);
  2041.     ;
  2042.     cmp    si,word ptr [bp+10]
  2043.     jge    short @9@482
  2044.     ;
  2045.     ;        if (x < minc2) {
  2046.     ;
  2047.     sub    ax,word ptr [bp+10]
  2048. .386
  2049. movsx    edx, ax
  2050. mov    [bp-34], edx
  2051.     ;
  2052.     ;          tdist = x - minc2;
  2053.     ;
  2054. imul    edx, edx
  2055. add    [bp-26], edx
  2056. .286
  2057.     ;
  2058.     ;          min_dist += tdist*tdist;
  2059.     ;
  2060.     mov    ax,si
  2061.     sub    ax,word ptr [bp-8]
  2062.     jmp    short @9@602
  2063. @9@482:
  2064.     ;
  2065.     ;          tdist = x - maxc2;
  2066.     ;          max_dist += tdist*tdist;
  2067.     ;
  2068.     cmp    si,word ptr [bp-8]
  2069.     jle    short @9@530
  2070.     ;
  2071.     ;        } else if (x > maxc2) {
  2072.     ;
  2073.     mov    ax,si
  2074.     sub    ax,word ptr [bp-8]
  2075. .386
  2076. movsx    edx, ax
  2077. mov    [bp-34], edx
  2078.     ;
  2079.     ;          tdist = x - maxc2;
  2080.     ;
  2081. imul    edx, edx
  2082. add    [bp-26], edx
  2083. .286
  2084.     jmp    short @9@578
  2085. @9@530:
  2086.     ;
  2087.     ;          min_dist += tdist*tdist;
  2088.     ;          tdist = x - minc2;
  2089.     ;          max_dist += tdist*tdist;
  2090.     ;        } else {
  2091.     ;          /* within cell range so no contribution to min_dist */
  2092.     ;
  2093.     cmp    si,word ptr [bp-14]
  2094.     jg    short @9@578
  2095.     ;
  2096.     ;          if (x <= centerc2) {
  2097.     ;
  2098.     mov    ax,si
  2099.     sub    ax,word ptr [bp-8]
  2100.     jmp    short @9@602
  2101. @9@578:
  2102.     ;
  2103.     ;        tdist = x - maxc2;
  2104.     ;        max_dist += tdist*tdist;
  2105.     ;          } else {
  2106.     ;
  2107.     mov    ax,si
  2108.     sub    ax,word ptr [bp+10]
  2109. @9@602:
  2110. .386
  2111. movsx    ecx, ax
  2112. mov    [bp-34], ecx
  2113.     ;
  2114.     ;        tdist = x - minc2;
  2115.     ;
  2116. imul    ecx, ecx
  2117. add    [bp-30], ecx
  2118. .286
  2119.     ;
  2120.     ;        max_dist += tdist*tdist;
  2121.     ;          }
  2122.     ;        }
  2123.     ;
  2124.     ;
  2125.     mov    bx,word ptr [bp-16]
  2126.     shl    bx,2
  2127.     lea    ax,word ptr [bp-1058]
  2128.     add    bx,ax
  2129.     mov    ax,word ptr [bp-24]
  2130.     mov    dx,word ptr [bp-26]
  2131.     mov    word ptr [bx+2],ax
  2132.     mov    word ptr [bx],dx
  2133.     ;
  2134.     ;        mindist[i] = min_dist;    /* save away the results */
  2135.     ;
  2136.     mov    ax,word ptr [bp-28]
  2137.     mov    dx,word ptr [bp-30]
  2138.     cmp    ax,word ptr [bp-20]
  2139.     jg    short @9@722
  2140.     jl    short @9@698
  2141.     cmp    dx,word ptr [bp-22]
  2142.     jae    short @9@722
  2143. @9@698:
  2144.    ;    
  2145.    ;        if (max_dist < minmaxdist)
  2146.     ;
  2147.     mov    ax,word ptr [bp-28]
  2148.     mov    dx,word ptr [bp-30]
  2149.     mov    word ptr [bp-20],ax
  2150.     mov    word ptr [bp-22],dx
  2151. @9@722:
  2152.     inc    word ptr [bp-16]
  2153. @9@746:
  2154.     mov    ax,word ptr [bp-16]
  2155.     cmp    ax,word ptr [bp-2]
  2156.     jge    @@7
  2157.     jmp    @9@50
  2158. @@7:
  2159.    ;    
  2160.    ;          minmaxdist = max_dist;
  2161.    ;      }
  2162.    ;    
  2163.    ;      /* Now we know that no cell in the update box is more than minmaxdist
  2164.    ;       * away from some colormap entry.  Therefore, only colors that are
  2165.     ;       * within minmaxdist of some part of the box need be considered.
  2166.    ;       */
  2167.    ;    
  2168.     mov    word ptr [bp-18],0
  2169.    ;    
  2170.    ;      ncolors = 0;
  2171.    ;    
  2172.     mov    word ptr [bp-16],0
  2173.     jmp    short @9@914
  2174. @9@794:
  2175.    ;    
  2176.    ;      for (i = 0; i < numcolors; i++) {
  2177.    ;    
  2178.     mov    bx,word ptr [bp-16]
  2179.     shl    bx,2
  2180.     lea    ax,word ptr [bp-1058]
  2181.     add    bx,ax
  2182.     mov    ax,word ptr [bx+2]
  2183.     mov    dx,word ptr [bx]
  2184.     cmp    ax,word ptr [bp-20]
  2185.     jg    short @9@890
  2186.     jne    short @9@866
  2187.     cmp    dx,word ptr [bp-22]
  2188.     ja    short @9@890
  2189. @9@866:
  2190.     ;
  2191.    ;        if (mindist[i] <= minmaxdist)
  2192.    ;    
  2193.     mov    bx,word ptr [bp+12]
  2194.     add    bx,word ptr [bp-18]
  2195.     mov    al,byte ptr [bp-16]
  2196.     mov    byte ptr [bx],al
  2197.     inc    word ptr [bp-18]
  2198. @9@890:
  2199.     inc    word ptr [bp-16]
  2200. @9@914:
  2201.     mov    ax,word ptr [bp-16]
  2202.     cmp    ax,word ptr [bp-2]
  2203.     jl    short @9@794
  2204.    ;    
  2205.    ;          colorlist[ncolors++] = (JSAMPLE) i;
  2206.    ;      }
  2207.    ;    
  2208.     mov    ax,word ptr [bp-18]
  2209.     ;
  2210.    ;      return ncolors;
  2211.    ;    
  2212.     pop    di
  2213.     pop    si
  2214.     leave    
  2215.     ret    
  2216. find_nearby_colors    endp
  2217.    ;    
  2218.     ;    LOCAL void
  2219.    ;    
  2220.     assume    cs:_TEXT
  2221. find_best_colors    proc    near
  2222.     push    bp
  2223.     mov    bp,sp
  2224.     sub    sp,558
  2225.     push    si
  2226.     push    di
  2227.    ;    
  2228.    ;    find_best_colors (decompress_info_ptr cinfo, int minc0, int minc1, int minc2,
  2229.    ;              int numcolors, JSAMPLE colorlist[], JSAMPLE bestcolor[])
  2230.    ;    /* Find the closest colormap entry for each cell in the update box,
  2231.    ;     * given the list of candidate colors prepared by find_nearby_colors.
  2232.    ;     * Return the indexes of the closest entries in the bestcolor[] array.
  2233.    ;     * This routine uses Thomas' incremental distance calculation method to
  2234.     ;     * find the distance from a colormap entry to successive cells in the box.
  2235.    ;     */
  2236.    ;    {
  2237.    ;      int ic0, ic1, ic2;
  2238.    ;      int i, icolor;
  2239.    ;      register INT32 * bptr;    /* pointer into bestdist[] array */
  2240.    ;      JSAMPLE * cptr;        /* pointer into bestcolor[] array */
  2241.    ;      INT32 dist0, dist1;        /* initial distance values */
  2242.    ;      register INT32 dist2;        /* current distance in inner loop */
  2243.    ;      INT32 xx0, xx1;        /* distance increments */
  2244.    ;      register INT32 xx2;
  2245.    ;      INT32 inc0, inc1, inc2;    /* initial values for increments */
  2246.    ;      /* This array holds the distance to the nearest-so-far color for each cell */
  2247.    ;      INT32 bestdist[BOX_Y_ELEMS * BOX_C_ELEMS * BOX_C_ELEMS];
  2248.    ;    
  2249.    ;      /* Initialize best-distance for each cell of the update box */
  2250.    ;    
  2251.     lea    ax,word ptr [bp-558]
  2252.     mov    si,ax
  2253.     ;
  2254.    ;      bptr = bestdist;
  2255.    ;    
  2256.     mov    word ptr [bp-8],127
  2257.     jmp    short @10@98
  2258. @10@50:
  2259.    ;    
  2260.    ;      for (i = BOX_Y_ELEMS*BOX_C_ELEMS*BOX_C_ELEMS-1; i >= 0; i--)
  2261.    ;    
  2262.     mov    word ptr [si+2],32767
  2263.     mov    word ptr [si],65535
  2264.     add    si,4
  2265.     dec    word ptr [bp-8]
  2266. @10@98:
  2267.     cmp    word ptr [bp-8],0
  2268.     jge    short @10@50
  2269.    ;    
  2270.     ;        *bptr++ = 0x7FFFFFFFL;
  2271.    ;      
  2272.    ;      /* For each color selected by find_nearby_colors,
  2273.    ;       * compute its distance to the center of each cell in the box.
  2274.    ;       * If that's less than best-so-far, update best distance and color number.
  2275.    ;       * Note we have to scale Y to get correct distance in scaled space.
  2276.    ;       */
  2277.    ;      
  2278.     ;      /* Nominal steps between cell centers ("x" in Thomas article) */
  2279.    ;    #define STEP_Y  ((1 << Y_SHIFT) * Y_SCALE)
  2280.    ;    #define STEP_C  (1 << C_SHIFT)
  2281.    ;      
  2282.    ;    
  2283.     mov    word ptr [bp-8],0
  2284.     jmp    @10@554
  2285. @10@146:
  2286.    ;    
  2287.    ;      for (i = 0; i < numcolors; i++) {
  2288.    ;    
  2289.     mov    bx,word ptr [bp+14]
  2290.     add    bx,word ptr [bp-8]
  2291.     mov    al,byte ptr [bx]
  2292.     mov    ah,0
  2293.     mov    word ptr [bp-10],ax
  2294.     ;
  2295.     ;        icolor = GETJSAMPLE(colorlist[i]);
  2296.     ;        /* Compute (square of) distance from minc0/c1/c2 to this color */
  2297.     ;
  2298.     mov    bx,word ptr DGROUP:my_colormap
  2299.     les    bx,dword ptr [bx]
  2300.     add    bx,word ptr [bp-10]
  2301.     mov    al,byte ptr es:[bx]
  2302.     mov    ah,0
  2303.     push    ax
  2304.     mov    ax,word ptr [bp+6]
  2305.     pop    dx
  2306.     sub    ax,dx
  2307.     shl    ax,1
  2308. .386
  2309. movsx    edx, ax
  2310. mov    [bp-38], edx
  2311.     ;
  2312.     ;        inc0 = (minc0 - (int) GETJSAMPLE(my_colormap[0][icolor])) * Y_SCALE;
  2313.     ;
  2314. imul    edx, edx
  2315. mov    [bp-14], edx
  2316. .286
  2317.     ;
  2318.     ;        dist0 = inc0*inc0;
  2319.     ;
  2320.     mov    bx,word ptr DGROUP:my_colormap
  2321.     les    bx,dword ptr [bx+4]
  2322.     add    bx,word ptr [bp-10]
  2323.     mov    al,byte ptr es:[bx]
  2324.     mov    ah,0
  2325.     push    ax
  2326.     mov    ax,word ptr [bp+8]
  2327.     pop    dx
  2328.     sub    ax,dx
  2329. .386
  2330. movsx    edx, ax
  2331. mov    [bp-42], edx
  2332.     ;
  2333.     ;        inc1 = minc1 - (int) GETJSAMPLE(my_colormap[1][icolor]);
  2334.     ;
  2335. imul    edx, edx
  2336. add    [bp-14], edx
  2337. .286
  2338.     ;
  2339.     ;        dist0 += inc1*inc1;
  2340.     ;
  2341.     mov    bx,word ptr DGROUP:my_colormap
  2342.     les    bx,dword ptr [bx+8]
  2343.     add    bx,word ptr [bp-10]
  2344.     mov    al,byte ptr es:[bx]
  2345.     mov    ah,0
  2346.     push    ax
  2347.     mov    ax,word ptr [bp+10]
  2348.     pop    dx
  2349.     sub    ax,dx
  2350. .386
  2351. movsx    edx, ax
  2352. mov    [bp-46], edx
  2353.     ;
  2354.     ;        inc2 = minc2 - (int) GETJSAMPLE(my_colormap[2][icolor]);
  2355.     ;
  2356. imul    edx, edx
  2357. add    [bp-14], edx
  2358.     ;
  2359.     ;        dist0 += inc2*inc2;
  2360.     ;        /* Form the initial difference increments */
  2361.     ;
  2362. mov    edx, [bp-38]
  2363. sal    edx, 4
  2364. add    edx, 64
  2365. mov    [bp-38], edx
  2366.     ;
  2367.     ;        inc0 = inc0 * (2 * STEP_Y) + STEP_Y * STEP_Y;
  2368.     ;
  2369. mov    eax, [bp-42]
  2370. sal    eax, 4
  2371. add    eax, 64
  2372. mov    [bp-42], eax
  2373.     ;
  2374.     ;        inc1 = inc1 * (2 * STEP_C) + STEP_C * STEP_C;
  2375.     ;
  2376. mov    edx, [bp-46]
  2377. sal    edx, 4
  2378. add    edx, 64
  2379. mov    [bp-46], edx
  2380. .286
  2381.     ;
  2382.     ;        inc2 = inc2 * (2 * STEP_C) + STEP_C * STEP_C;
  2383.     ;        /* Now loop over all cells in box, updating distance per Thomas method */
  2384.     ;
  2385.     lea    ax,word ptr [bp-558]
  2386.     mov    si,ax
  2387.     ;
  2388.     ;        bptr = bestdist;
  2389.     ;
  2390.     mov    di,word ptr [bp+16]
  2391.    ;    
  2392.    ;        cptr = bestcolor;
  2393.    ;    
  2394.     mov    ax,word ptr [bp-36]
  2395.     mov    dx,word ptr [bp-38]
  2396.     mov    word ptr [bp-24],ax
  2397.     mov    word ptr [bp-26],dx
  2398.    ;    
  2399.    ;        xx0 = inc0;
  2400.    ;    
  2401.     mov    word ptr [bp-2],7
  2402.     jmp    @10@506
  2403. @10@170:
  2404.    ;    
  2405.    ;        for (ic0 = BOX_Y_ELEMS-1; ic0 >= 0; ic0--) {
  2406.     ;
  2407.     mov    ax,word ptr [bp-12]
  2408.     mov    dx,word ptr [bp-14]
  2409.     mov    word ptr [bp-16],ax
  2410.     mov    word ptr [bp-18],dx
  2411.     ;
  2412.     ;          dist1 = dist0;
  2413.     ;
  2414.     mov    ax,word ptr [bp-40]
  2415.     mov    dx,word ptr [bp-42]
  2416.     mov    word ptr [bp-28],ax
  2417.     mov    word ptr [bp-30],dx
  2418.    ;    
  2419.    ;          xx1 = inc1;
  2420.    ;    
  2421.     mov    word ptr [bp-4],3
  2422.     jmp    short @10@434
  2423. @10@194:
  2424.    ;    
  2425.    ;          for (ic1 = BOX_C_ELEMS-1; ic1 >= 0; ic1--) {
  2426.    ;    
  2427.     mov    ax,word ptr [bp-16]
  2428.     mov    dx,word ptr [bp-18]
  2429.     mov    word ptr [bp-20],ax
  2430.     mov    word ptr [bp-22],dx
  2431.    ;    
  2432.     ;        dist2 = dist1;
  2433.    ;    
  2434.     mov    ax,word ptr [bp-44]
  2435.     mov    dx,word ptr [bp-46]
  2436.     mov    word ptr [bp-32],ax
  2437.     mov    word ptr [bp-34],dx
  2438.    ;    
  2439.    ;        xx2 = inc2;
  2440.    ;    
  2441.     mov    word ptr [bp-6],3
  2442.     jmp    short @10@362
  2443. @10@218:
  2444.    ;    
  2445.    ;        for (ic2 = BOX_C_ELEMS-1; ic2 >= 0; ic2--) {
  2446.    ;    
  2447.     mov    ax,word ptr [si+2]
  2448.     mov    dx,word ptr [si]
  2449.     cmp    ax,word ptr [bp-20]
  2450.     jl    short @10@314
  2451.     jg    short @10@290
  2452.     cmp    dx,word ptr [bp-22]
  2453.     jbe    short @10@314
  2454. @10@290:
  2455.    ;    
  2456.    ;          if (dist2 < *bptr) {
  2457.    ;    
  2458.     mov    ax,word ptr [bp-20]
  2459.     mov    dx,word ptr [bp-22]
  2460.     mov    word ptr [si+2],ax
  2461.     mov    word ptr [si],dx
  2462.    ;    
  2463.    ;            *bptr = dist2;
  2464.    ;    
  2465.     mov    al,byte ptr [bp-10]
  2466.     mov    byte ptr [di],al
  2467. @10@314:
  2468.    ;    
  2469.    ;            *cptr = (JSAMPLE) icolor;
  2470.    ;          }
  2471.    ;    
  2472.     mov    ax,word ptr [bp-32]
  2473.     mov    dx,word ptr [bp-34]
  2474.     add    word ptr [bp-22],dx
  2475.     adc    word ptr [bp-20],ax
  2476.     ;
  2477.    ;          dist2 += xx2;
  2478.    ;    
  2479.     add    word ptr [bp-34],128
  2480.     adc    word ptr [bp-32],0
  2481.    ;    
  2482.    ;          xx2 += 2 * STEP_C * STEP_C;
  2483.    ;    
  2484.     add    si,4
  2485.    ;    
  2486.    ;          bptr++;
  2487.    ;    
  2488.     inc    di
  2489.     dec    word ptr [bp-6]
  2490. @10@362:
  2491.     cmp    word ptr [bp-6],0
  2492.     jge    short @10@218
  2493.    ;    
  2494.    ;          cptr++;
  2495.    ;        }
  2496.     ;
  2497.     mov    ax,word ptr [bp-28]
  2498.     mov    dx,word ptr [bp-30]
  2499.     add    word ptr [bp-18],dx
  2500.     adc    word ptr [bp-16],ax
  2501.    ;    
  2502.    ;        dist1 += xx1;
  2503.    ;    
  2504.     add    word ptr [bp-30],128
  2505.     adc    word ptr [bp-28],0
  2506.     dec    word ptr [bp-4]
  2507. @10@434:
  2508.     cmp    word ptr [bp-4],0
  2509.     jge    short @10@194
  2510.    ;    
  2511.    ;        xx1 += 2 * STEP_C * STEP_C;
  2512.    ;          }
  2513.    ;    
  2514.     mov    ax,word ptr [bp-24]
  2515.     mov    dx,word ptr [bp-26]
  2516.     add    word ptr [bp-14],dx
  2517.     adc    word ptr [bp-12],ax
  2518.    ;    
  2519.    ;          dist0 += xx0;
  2520.     ;
  2521.     add    word ptr [bp-26],128
  2522.     adc    word ptr [bp-24],0
  2523.     dec    word ptr [bp-2]
  2524. @10@506:
  2525.     cmp    word ptr [bp-2],0
  2526.     jl    @@8
  2527.     jmp    @10@170
  2528. @@8:
  2529.     inc    word ptr [bp-8]
  2530. @10@554:
  2531.     mov    ax,word ptr [bp-8]
  2532.     cmp    ax,word ptr [bp+12]
  2533.     jge    @@9
  2534.     jmp    @10@146
  2535. @@9:
  2536.    ;    
  2537.    ;          xx0 += 2 * STEP_Y * STEP_Y;
  2538.    ;        }
  2539.    ;      }
  2540.     ;
  2541.     pop    di
  2542.     pop    si
  2543.     leave    
  2544.     ret    
  2545. find_best_colors    endp
  2546.    ;    
  2547.    ;    LOCAL void
  2548.    ;    
  2549.     assume    cs:_TEXT
  2550. fill_inverse_cmap    proc    near
  2551.     push    bp
  2552.     mov    bp,sp
  2553.     sub    sp,400
  2554.     push    si
  2555.     push    di
  2556.    ;    
  2557.    ;    fill_inverse_cmap (decompress_info_ptr cinfo, int c0, int c1, int c2)
  2558.    ;    /* Fill the inverse-colormap entries in the update box that contains */
  2559.    ;    /* histogram cell c0/c1/c2.  (Only that one cell MUST be filled, but */
  2560.    ;    /* we can fill as many others as we wish.) */
  2561.    ;    {
  2562.    ;      int minc0, minc1, minc2;    /* lower left corner of update box */
  2563.    ;      int ic0, ic1, ic2;
  2564.     ;      register JSAMPLE * cptr;    /* pointer into bestcolor[] array */
  2565.    ;      register histptr cachep;    /* pointer into main cache array */
  2566.    ;      /* This array lists the candidate colormap indexes. */
  2567.    ;      JSAMPLE colorlist[MAXNUMCOLORS];
  2568.    ;      int numcolors;        /* number of candidate colors */
  2569.    ;      /* This array holds the actually closest colormap index for each cell. */
  2570.    ;      JSAMPLE bestcolor[BOX_Y_ELEMS * BOX_C_ELEMS * BOX_C_ELEMS];
  2571.    ;    
  2572.    ;      /* Convert cell coordinates to update box ID */
  2573.    ;    
  2574.     sar    word ptr [bp+6],3
  2575.    ;    
  2576.    ;      c0 >>= BOX_Y_LOG;
  2577.    ;    
  2578.     sar    word ptr [bp+8],2
  2579.    ;    
  2580.    ;      c1 >>= BOX_C_LOG;
  2581.    ;    
  2582.     sar    word ptr [bp+10],2
  2583.    ;    
  2584.     ;      c2 >>= BOX_C_LOG;
  2585.    ;    
  2586.     ;      /* Compute true coordinates of update box's origin corner.
  2587.    ;       * Actually we compute the coordinates of the center of the corner
  2588.    ;       * histogram cell, which are the lower bounds of the volume we care about.
  2589.    ;       */
  2590.    ;    
  2591.     mov    ax,word ptr [bp+6]
  2592.     shl    ax,5
  2593.     inc    ax
  2594.     inc    ax
  2595.     mov    word ptr [bp-2],ax
  2596.    ;    
  2597.    ;      minc0 = (c0 << BOX_Y_SHIFT) + ((1 << Y_SHIFT) >> 1);
  2598.    ;    
  2599.     mov    ax,word ptr [bp+8]
  2600.     shl    ax,5
  2601.     add    ax,4
  2602.     mov    word ptr [bp-4],ax
  2603.    ;    
  2604.    ;      minc1 = (c1 << BOX_C_SHIFT) + ((1 << C_SHIFT) >> 1);
  2605.    ;    
  2606.     mov    ax,word ptr [bp+10]
  2607.     shl    ax,5
  2608.     add    ax,4
  2609.     mov    word ptr [bp-6],ax
  2610.    ;    
  2611.    ;      minc2 = (c2 << BOX_C_SHIFT) + ((1 << C_SHIFT) >> 1);
  2612.    ;      
  2613.    ;      /* Determine which colormap entries are close enough to be candidates
  2614.    ;       * for the nearest entry to some cell in the update box.
  2615.    ;       */
  2616.    ;    
  2617.     lea    ax,word ptr [bp-272]
  2618.     push    ax
  2619.     push    word ptr [bp-6]
  2620.     push    word ptr [bp-4]
  2621.     push    word ptr [bp-2]
  2622.     push    word ptr [bp+4]
  2623.     call    near ptr find_nearby_colors
  2624.     add    sp,10
  2625.     mov    word ptr [bp-16],ax
  2626.    ;    
  2627.    ;      numcolors = find_nearby_colors(cinfo, minc0, minc1, minc2, colorlist);
  2628.     ;
  2629.    ;      /* Determine the actually nearest colors. */
  2630.     ;
  2631.    ;    
  2632.    ;      find_best_colors(cinfo, minc0, minc1, minc2, numcolors, colorlist,
  2633.    ;    
  2634.     lea    ax,word ptr [bp-400]
  2635.     push    ax
  2636.     lea    ax,word ptr [bp-272]
  2637.     push    ax
  2638.     push    word ptr [bp-16]
  2639.     push    word ptr [bp-6]
  2640.     push    word ptr [bp-4]
  2641.     push    word ptr [bp-2]
  2642.     push    word ptr [bp+4]
  2643.     call    near ptr find_best_colors
  2644.     add    sp,14
  2645.    ;    
  2646.    ;               bestcolor);
  2647.    ;    
  2648.    ;      /* Save the best color numbers (plus 1) in the main cache array */
  2649.    ;    
  2650.     shl    word ptr [bp+6],3
  2651.    ;    
  2652.     ;      c0 <<= BOX_Y_LOG;        /* convert ID back to base cell indexes */
  2653.    ;    
  2654.     shl    word ptr [bp+8],2
  2655.    ;    
  2656.    ;      c1 <<= BOX_C_LOG;
  2657.    ;    
  2658.     shl    word ptr [bp+10],2
  2659.    ;    
  2660.    ;      c2 <<= BOX_C_LOG;
  2661.    ;    
  2662.     lea    ax,word ptr [bp-400]
  2663.     mov    si,ax
  2664.    ;    
  2665.    ;      cptr = bestcolor;
  2666.    ;    
  2667.     mov    word ptr [bp-8],0
  2668.     jmp    short @11@242
  2669. @11@50:
  2670.    ;    
  2671.    ;      for (ic0 = 0; ic0 < BOX_Y_ELEMS; ic0++) {
  2672.     ;
  2673.     mov    word ptr [bp-10],0
  2674.     jmp    short @11@194
  2675. @11@74:
  2676.    ;    
  2677.    ;        for (ic1 = 0; ic1 < BOX_C_ELEMS; ic1++) {
  2678.    ;    
  2679.     mov    ax,word ptr [bp+6]
  2680.     add    ax,word ptr [bp-8]
  2681.     shl    ax,2
  2682.     mov    bx,word ptr DGROUP:histogram
  2683.     add    bx,ax
  2684.     mov    ax,word ptr [bx+2]
  2685.     mov    dx,word ptr [bx]
  2686.     mov    bx,word ptr [bp+8]
  2687.     add    bx,word ptr [bp-10]
  2688.     shl    bx,6
  2689.     add    dx,bx
  2690.     mov    bx,word ptr [bp+10]
  2691.     shl    bx,1
  2692.     add    dx,bx
  2693.     mov    word ptr [bp-12],ax
  2694.     mov    word ptr [bp-14],dx
  2695.    ;    
  2696.     ;          cachep = & histogram[c0+ic0][c1+ic1][c2];
  2697.    ;    
  2698.     xor    di,di
  2699.     jmp    short @11@146
  2700. @11@98:
  2701.    ;    
  2702.    ;          for (ic2 = 0; ic2 < BOX_C_ELEMS; ic2++) {
  2703.    ;    
  2704.     mov    al,byte ptr [si]
  2705.     mov    ah,0
  2706.     inc    ax
  2707.     les    bx,dword ptr [bp-14]
  2708.     mov    word ptr es:[bx],ax
  2709.     inc    si
  2710.     add    word ptr [bp-14],2
  2711.     inc    di
  2712. @11@146:
  2713.     cmp    di,4
  2714.     jl    short @11@98
  2715.     inc    word ptr [bp-10]
  2716. @11@194:
  2717.     cmp    word ptr [bp-10],4
  2718.     jl    short @11@74
  2719.     inc    word ptr [bp-8]
  2720. @11@242:
  2721.     cmp    word ptr [bp-8],8
  2722.     jl    short @11@50
  2723.    ;    
  2724.    ;        *cachep++ = (histcell) (GETJSAMPLE(*cptr++) + 1);
  2725.    ;          }
  2726.    ;        }
  2727.    ;      }
  2728.    ;    
  2729.     pop    di
  2730.     pop    si
  2731.     leave    
  2732.     ret    
  2733. fill_inverse_cmap    endp
  2734.    ;    
  2735.    ;    METHODDEF void
  2736.    ;    
  2737.     assume    cs:_TEXT
  2738. pass2_nodither    proc    near
  2739.     push    bp
  2740.     mov    bp,sp
  2741.     sub    sp,32
  2742.     push    si
  2743.     push    di
  2744.    ;    
  2745.    ;    pass2_nodither (decompress_info_ptr cinfo, int num_rows,
  2746.    ;            JSAMPIMAGE image_data, JSAMPARRAY output_workspace)
  2747.    ;    /* This version performs no dithering */
  2748.    ;    {
  2749.    ;      register JSAMPROW ptr0, ptr1, ptr2, outptr;
  2750.    ;      register histptr cachep;
  2751.    ;      register int c0, c1, c2;
  2752.    ;      int row;
  2753.    ;      long col;
  2754.    ;    
  2755.     mov    bx,word ptr [bp+4]
  2756.     mov    ax,word ptr [bx+38]
  2757.     mov    dx,word ptr [bx+36]
  2758.     mov    word ptr [bp-30],ax
  2759.     mov    word ptr [bp-32],dx
  2760.     ;
  2761.    ;      long width = cinfo->image_width;
  2762.     ;
  2763.    ;      /* Convert data to colormap indexes, which we save in output_workspace */
  2764.    ;    
  2765.     mov    word ptr [bp-24],0
  2766.     jmp    @12@266
  2767. @12@50:
  2768.    ;    
  2769.    ;      for (row = 0; row < num_rows; row++) {
  2770.    ;    
  2771.     mov    ax,word ptr [bp-24]
  2772.     shl    ax,2
  2773.     mov    bx,word ptr [bp+8]
  2774.     mov    bx,word ptr [bx]
  2775.     add    bx,ax
  2776.     mov    ax,word ptr [bx+2]
  2777.     mov    dx,word ptr [bx]
  2778.     mov    word ptr [bp-2],ax
  2779.     mov    word ptr [bp-4],dx
  2780.    ;    
  2781.    ;        ptr0 = image_data[0][row];
  2782.    ;    
  2783.     mov    ax,word ptr [bp-24]
  2784.     shl    ax,2
  2785.     mov    bx,word ptr [bp+8]
  2786.     mov    bx,word ptr [bx+2]
  2787.     add    bx,ax
  2788.     mov    ax,word ptr [bx+2]
  2789.     mov    dx,word ptr [bx]
  2790.     mov    word ptr [bp-6],ax
  2791.     mov    word ptr [bp-8],dx
  2792.    ;    
  2793.    ;        ptr1 = image_data[1][row];
  2794.    ;    
  2795.     mov    ax,word ptr [bp-24]
  2796.     shl    ax,2
  2797.     mov    bx,word ptr [bp+8]
  2798.     mov    bx,word ptr [bx+4]
  2799.     add    bx,ax
  2800.     mov    ax,word ptr [bx+2]
  2801.     mov    dx,word ptr [bx]
  2802.     mov    word ptr [bp-10],ax
  2803.     mov    word ptr [bp-12],dx
  2804.     ;
  2805.    ;        ptr2 = image_data[2][row];
  2806.     ;
  2807.     mov    ax,word ptr [bp-24]
  2808.     shl    ax,2
  2809.     mov    bx,word ptr [bp+10]
  2810.     add    bx,ax
  2811.     mov    ax,word ptr [bx+2]
  2812.     mov    dx,word ptr [bx]
  2813.     mov    word ptr [bp-14],ax
  2814.     mov    word ptr [bp-16],dx
  2815.    ;    
  2816.    ;        outptr = output_workspace[row];
  2817.    ;    
  2818.     mov    ax,word ptr [bp-30]
  2819.     mov    dx,word ptr [bp-32]
  2820.     mov    word ptr [bp-26],ax
  2821.     mov    word ptr [bp-28],dx
  2822.     jmp    @12@170
  2823. @12@74:
  2824.    ;    
  2825.    ;        for (col = width; col > 0; col--) {
  2826.    ;          /* get pixel value and index into the cache */
  2827.    ;    
  2828.     les    bx,dword ptr [bp-4]
  2829.     mov    al,byte ptr es:[bx]
  2830.     mov    ah,0
  2831.     sar    ax,2
  2832.     mov    si,ax
  2833.     inc    word ptr [bp-4]
  2834.    ;    
  2835.    ;          c0 = GETJSAMPLE(*ptr0++) >> Y_SHIFT;
  2836.    ;    
  2837.     les    bx,dword ptr [bp-8]
  2838.     mov    al,byte ptr es:[bx]
  2839.     mov    ah,0
  2840.     sar    ax,3
  2841.     mov    di,ax
  2842.     inc    word ptr [bp-8]
  2843.    ;    
  2844.    ;          c1 = GETJSAMPLE(*ptr1++) >> C_SHIFT;
  2845.    ;    
  2846.     les    bx,dword ptr [bp-12]
  2847.     mov    al,byte ptr es:[bx]
  2848.     mov    ah,0
  2849.     sar    ax,3
  2850.     mov    word ptr [bp-22],ax
  2851.     inc    word ptr [bp-12]
  2852.    ;    
  2853.    ;          c2 = GETJSAMPLE(*ptr2++) >> C_SHIFT;
  2854.    ;    
  2855.     mov    ax,si
  2856.     shl    ax,2
  2857.     mov    bx,word ptr DGROUP:histogram
  2858.     add    bx,ax
  2859.     mov    ax,word ptr [bx+2]
  2860.     mov    dx,word ptr [bx]
  2861.     mov    bx,di
  2862.     shl    bx,6
  2863.     add    dx,bx
  2864.     mov    bx,word ptr [bp-22]
  2865.     shl    bx,1
  2866.     add    dx,bx
  2867.     mov    word ptr [bp-18],ax
  2868.     mov    word ptr [bp-20],dx
  2869.    ;    
  2870.    ;          cachep = & histogram[c0][c1][c2];
  2871.    ;          /* If we have not seen this color before, find nearest colormap entry */
  2872.     ;          /* and update the cache */
  2873.    ;    
  2874.     les    bx,dword ptr [bp-20]
  2875.     cmp    word ptr es:[bx],0
  2876.     jne    short @12@122
  2877.    ;    
  2878.    ;          if (*cachep == 0)
  2879.    ;    
  2880.     push    word ptr [bp-22]
  2881.     push    di
  2882.     push    si
  2883.     push    word ptr [bp+4]
  2884.     call    near ptr fill_inverse_cmap
  2885.     add    sp,8
  2886. @12@122:
  2887.    ;    
  2888.    ;        fill_inverse_cmap(cinfo, c0,c1,c2);
  2889.    ;          /* Now emit the colormap index for this cell */
  2890.    ;    
  2891.     les    bx,dword ptr [bp-20]
  2892.     mov    al,byte ptr es:[bx]
  2893.     dec    al
  2894.     les    bx,dword ptr [bp-16]
  2895.     mov    byte ptr es:[bx],al
  2896.     inc    word ptr [bp-16]
  2897.     sub    word ptr [bp-28],1
  2898.     sbb    word ptr [bp-26],0
  2899. @12@170:
  2900.     cmp    word ptr [bp-26],0
  2901.     jle    @@10
  2902.     jmp    @12@74
  2903. @@10:
  2904.     jne    short @12@242
  2905.     cmp    word ptr [bp-28],0
  2906.     jbe    @@11
  2907.     jmp    @12@74
  2908. @@11:
  2909. @12@242:
  2910.     inc    word ptr [bp-24]
  2911. @12@266:
  2912.     mov    ax,word ptr [bp-24]
  2913.     cmp    ax,word ptr [bp+6]
  2914.     jge    @@12
  2915.     jmp    @12@50
  2916. @@12:
  2917.    ;    
  2918.    ;          *outptr++ = (JSAMPLE) (*cachep - 1);
  2919.    ;        }
  2920.    ;      }
  2921.    ;      /* Emit converted rows to the output file */
  2922.    ;    
  2923.     lea    ax,word ptr [bp+10]
  2924.     push    ax
  2925.     push    word ptr [bp+6]
  2926.     push    word ptr [bp+4]
  2927.     mov    bx,word ptr [bp+4]
  2928.     mov    bx,word ptr [bx]
  2929.     call    word ptr [bx+62]
  2930.     add    sp,6
  2931.    ;    
  2932.    ;      (*cinfo->methods->put_pixel_rows) (cinfo, num_rows, &output_workspace);
  2933.    ;    
  2934.     pop    di
  2935.     pop    si
  2936.     leave
  2937.     ret    
  2938. pass2_nodither    endp
  2939.    ;    
  2940.    ;    METHODDEF void
  2941.    ;    
  2942.     assume    cs:_TEXT
  2943. pass2_dither    proc    near
  2944.     push    bp
  2945.     mov    bp,sp
  2946.     sub    sp,46
  2947.     push    si
  2948.     push    di
  2949.    ;    
  2950.    ;    pass2_dither (decompress_info_ptr cinfo, int num_rows,
  2951.    ;              JSAMPIMAGE image_data, JSAMPARRAY output_workspace)
  2952.    ;    /* This version performs Floyd-Steinberg dithering */
  2953.    ;    {
  2954.    ;      register FSERROR val;
  2955.    ;      register FSERRPTR thisrowerr, nextrowerr;
  2956.    ;      register FSERROR c0, c1, c2;
  2957.    ;      register int pixcode;
  2958.    ;      JSAMPROW ptr0, ptr1, ptr2, outptr;
  2959.    ;      histptr cachep;
  2960.     ;      int dir;
  2961.    ;      long col;
  2962.    ;      int row;
  2963.    ;    
  2964.     mov    bx,word ptr [bp+4]
  2965.     mov    ax,word ptr [bx+38]
  2966.     mov    dx,word ptr [bx+36]
  2967.     mov    word ptr [bp-44],ax
  2968.     mov    word ptr [bp-46],dx
  2969.    ;    
  2970.    ;      long width = cinfo->image_width;
  2971.    ;    
  2972.    ;      /* Convert data to colormap indexes, which we save in output_workspace */
  2973.    ;    
  2974.     mov    word ptr [bp-42],0
  2975.     jmp    @13@626
  2976. @13@50:
  2977.    ;    
  2978.    ;      for (row = 0; row < num_rows; row++) {
  2979.    ;    
  2980.     mov    ax,word ptr [bp-42]
  2981.     shl    ax,2
  2982.     mov    bx,word ptr [bp+8]
  2983.     mov    bx,word ptr [bx]
  2984.     add    bx,ax
  2985.     mov    ax,word ptr [bx+2]
  2986.     mov    dx,word ptr [bx]
  2987.     mov    word ptr [bp-16],ax
  2988.     mov    word ptr [bp-18],dx
  2989.    ;    
  2990.    ;        ptr0 = image_data[0][row];
  2991.    ;    
  2992.     mov    ax,word ptr [bp-42]
  2993.     shl    ax,2
  2994.     mov    bx,word ptr [bp+8]
  2995.     mov    bx,word ptr [bx+2]
  2996.     add    bx,ax
  2997.     mov    ax,word ptr [bx+2]
  2998.     mov    dx,word ptr [bx]
  2999.     mov    word ptr [bp-20],ax
  3000.     mov    word ptr [bp-22],dx
  3001.    ;    
  3002.    ;        ptr1 = image_data[1][row];
  3003.    ;    
  3004.     mov    ax,word ptr [bp-42]
  3005.     shl    ax,2
  3006.     mov    bx,word ptr [bp+8]
  3007.     mov    bx,word ptr [bx+4]
  3008.     add    bx,ax
  3009.     mov    ax,word ptr [bx+2]
  3010.     mov    dx,word ptr [bx]
  3011.     mov    word ptr [bp-24],ax
  3012.     mov    word ptr [bp-26],dx
  3013.    ;    
  3014.    ;        ptr2 = image_data[2][row];
  3015.    ;    
  3016.     mov    ax,word ptr [bp-42]
  3017.     shl    ax,2
  3018.     mov    bx,word ptr [bp+10]
  3019.     add    bx,ax
  3020.     mov    ax,word ptr [bx+2]
  3021.     mov    dx,word ptr [bx]
  3022.     mov    word ptr [bp-28],ax
  3023.     mov    word ptr [bp-30],dx
  3024.     ;
  3025.    ;        outptr = output_workspace[row];
  3026.     ;
  3027.     cmp    word ptr DGROUP:on_odd_row,0
  3028.     je    short @13@98
  3029.    ;    
  3030.    ;        if (on_odd_row) {
  3031.    ;          /* work right to left in this row */
  3032.    ;    
  3033.     mov    ax,word ptr [bp-46]
  3034.     dec    ax
  3035.     add    word ptr [bp-18],ax
  3036.    ;    
  3037.    ;          ptr0 += width - 1;
  3038.    ;    
  3039.     mov    ax,word ptr [bp-46]
  3040.     dec    ax
  3041.     add    word ptr [bp-22],ax
  3042.    ;    
  3043.    ;          ptr1 += width - 1;
  3044.    ;    
  3045.     mov    ax,word ptr [bp-46]
  3046.     dec    ax
  3047.     add    word ptr [bp-26],ax
  3048.     ;
  3049.    ;          ptr2 += width - 1;
  3050.    ;    
  3051.     mov    ax,word ptr [bp-46]
  3052.     dec    ax
  3053.     add    word ptr [bp-30],ax
  3054.    ;    
  3055.    ;          outptr += width - 1;
  3056.    ;    
  3057.     mov    word ptr [bp-36],65535
  3058.    ;    
  3059.    ;          dir = -1;
  3060.    ;    
  3061.     mov    ax,word ptr DGROUP:oddrowerrs+2
  3062.     mov    dx,word ptr DGROUP:oddrowerrs
  3063.     add    dx,6
  3064.     mov    word ptr [bp-2],ax
  3065.     mov    word ptr [bp-4],dx
  3066.    ;    
  3067.    ;          thisrowerr = oddrowerrs + 3;
  3068.     ;
  3069.     mov    cx,word ptr [bp-44]
  3070.     mov    bx,word ptr [bp-46]
  3071.     xor    dx,dx
  3072.     mov    ax,3
  3073.     call    near ptr N_LXMUL@
  3074.     shl    ax,1
  3075.     mov    dx,word ptr DGROUP:evenrowerrs+2
  3076.     mov    bx,word ptr DGROUP:evenrowerrs
  3077.     add    bx,ax
  3078.     mov    word ptr [bp-6],dx
  3079.     mov    word ptr [bp-8],bx
  3080.    ;    
  3081.    ;          nextrowerr = evenrowerrs + width*3;
  3082.    ;    
  3083.     mov    word ptr DGROUP:on_odd_row,0
  3084.    ;    
  3085.    ;          on_odd_row = FALSE;    /* flip for next time */
  3086.    ;    
  3087.     jmp    short @13@122
  3088. @13@98:
  3089.    ;    
  3090.    ;        } else {
  3091.    ;          /* work left to right in this row */
  3092.     ;
  3093.     mov    word ptr [bp-36],1
  3094.    ;    
  3095.    ;          dir = 1;
  3096.    ;    
  3097.     mov    ax,word ptr DGROUP:evenrowerrs+2
  3098.     mov    dx,word ptr DGROUP:evenrowerrs
  3099.     add    dx,6
  3100.     mov    word ptr [bp-2],ax
  3101.     mov    word ptr [bp-4],dx
  3102.    ;    
  3103.    ;          thisrowerr = evenrowerrs + 3;
  3104.    ;    
  3105.     mov    cx,word ptr [bp-44]
  3106.     mov    bx,word ptr [bp-46]
  3107.     xor    dx,dx
  3108.     mov    ax,3
  3109.     call    near ptr N_LXMUL@
  3110.     shl    ax,1
  3111.     mov    dx,word ptr DGROUP:oddrowerrs+2
  3112.     mov    bx,word ptr DGROUP:oddrowerrs
  3113.     add    bx,ax
  3114.     mov    word ptr [bp-6],dx
  3115.     mov    word ptr [bp-8],bx
  3116.    ;    
  3117.    ;          nextrowerr = oddrowerrs + width*3;
  3118.    ;    
  3119.     mov    word ptr DGROUP:on_odd_row,1
  3120. @13@122:
  3121.    ;    
  3122.    ;          on_odd_row = TRUE;    /* flip for next time */
  3123.    ;        }
  3124.    ;        /* need only initialize this one entry in nextrowerr */
  3125.    ;    
  3126.     les    bx,dword ptr [bp-8]
  3127.     xor    ax,ax
  3128.     mov    word ptr es:[bx+4],ax
  3129.     mov    word ptr es:[bx+2],ax
  3130.     mov    word ptr es:[bx],ax
  3131.    ;    
  3132.    ;        nextrowerr[0] = nextrowerr[1] = nextrowerr[2] = 0;
  3133.    ;    
  3134.     mov    ax,word ptr [bp-44]
  3135.     mov    dx,word ptr [bp-46]
  3136.     mov    word ptr [bp-38],ax
  3137.     mov    word ptr [bp-40],dx
  3138.     jmp    @13@530
  3139. @13@146:
  3140.    ;    
  3141.    ;        for (col = width; col > 0; col--) {
  3142.    ;          /* Get this pixel's value and add accumulated errors */
  3143.    ;          /* The errors are in units of 1/16th pixel value */
  3144.    ;    
  3145.     les    bx,dword ptr [bp-18]
  3146.     mov    al,byte ptr es:[bx]
  3147.     mov    ah,0
  3148.     shl    ax,4
  3149.     les    bx,dword ptr [bp-4]
  3150.     add    ax,word ptr es:[bx]
  3151.     mov    si,ax
  3152.    ;    
  3153.    ;          val = (GETJSAMPLE(*ptr0) << 4) + thisrowerr[0];
  3154.    ;    
  3155.     or    si,si
  3156.     jg    short @13@194
  3157.     xor    si,si
  3158.     jmp    short @13@242
  3159. @13@194:
  3160.    ;    
  3161.    ;          if (val <= 0) val = 0;    /* must watch for range overflow! */
  3162.    ;          else {
  3163.    ;    
  3164.     add    si,8
  3165.    ;    
  3166.    ;        val += 8;        /* divide by 16 with proper rounding */
  3167.    ;    
  3168.     sar    si,4
  3169.    ;    
  3170.    ;        val >>= 4;
  3171.    ;    
  3172.     cmp    si,255
  3173.     jle    short @13@242
  3174.     mov    si,255
  3175. @13@242:
  3176.    ;    
  3177.    ;        if (val > MAXJSAMPLE) val = MAXJSAMPLE;
  3178.    ;          }
  3179.    ;    
  3180.     mov    di,si
  3181.    ;    
  3182.    ;          c0 = val;
  3183.    ;    
  3184.     les    bx,dword ptr [bp-22]
  3185.     mov    al,byte ptr es:[bx]
  3186.     mov    ah,0
  3187.     shl    ax,4
  3188.     les    bx,dword ptr [bp-4]
  3189.     add    ax,word ptr es:[bx+2]
  3190.     mov    si,ax
  3191.    ;    
  3192.    ;          val = (GETJSAMPLE(*ptr1) << 4) + thisrowerr[1];
  3193.    ;    
  3194.     or    si,si
  3195.     jg    short @13@290
  3196.     xor    si,si
  3197.     jmp    short @13@338
  3198. @13@290:
  3199.    ;    
  3200.     ;          if (val <= 0) val = 0;    /* must watch for range overflow! */
  3201.    ;          else {
  3202.    ;    
  3203.     add    si,8
  3204.    ;    
  3205.    ;        val += 8;        /* divide by 16 with proper rounding */
  3206.    ;    
  3207.     sar    si,4
  3208.    ;    
  3209.    ;        val >>= 4;
  3210.    ;    
  3211.     cmp    si,255
  3212.     jle    short @13@338
  3213.     mov    si,255
  3214. @13@338:
  3215.    ;    
  3216.    ;        if (val > MAXJSAMPLE) val = MAXJSAMPLE;
  3217.    ;          }
  3218.    ;    
  3219.     mov    word ptr [bp-10],si
  3220.    ;    
  3221.    ;          c1 = val;
  3222.    ;    
  3223.     les    bx,dword ptr [bp-26]
  3224.     mov    al,byte ptr es:[bx]
  3225.     mov    ah,0
  3226.     shl    ax,4
  3227.     les    bx,dword ptr [bp-4]
  3228.     add    ax,word ptr es:[bx+4]
  3229.     mov    si,ax
  3230.    ;    
  3231.    ;          val = (GETJSAMPLE(*ptr2) << 4) + thisrowerr[2];
  3232.    ;    
  3233.     or    si,si
  3234.     jg    short @13@386
  3235.     xor    si,si
  3236.     jmp    short @13@434
  3237. @13@386:
  3238.    ;    
  3239.    ;          if (val <= 0) val = 0;    /* must watch for range overflow! */
  3240.    ;          else {
  3241.    ;    
  3242.     add    si,8
  3243.    ;    
  3244.     ;        val += 8;        /* divide by 16 with proper rounding */
  3245.    ;    
  3246.     sar    si,4
  3247.    ;    
  3248.    ;        val >>= 4;
  3249.    ;    
  3250.     cmp    si,255
  3251.     jle    short @13@434
  3252.     mov    si,255
  3253. @13@434:
  3254.    ;    
  3255.    ;        if (val > MAXJSAMPLE) val = MAXJSAMPLE;
  3256.    ;          }
  3257.    ;    
  3258.     mov    word ptr [bp-12],si
  3259.    ;    
  3260.    ;          c2 = val;
  3261.    ;          /* Index into the cache with adjusted value */
  3262.    ;    
  3263.     mov    ax,di
  3264.     sar    ax,2
  3265.     shl    ax,2
  3266.     mov    bx,word ptr DGROUP:histogram
  3267.     add    bx,ax
  3268.     mov    ax,word ptr [bx+2]
  3269.     mov    dx,word ptr [bx]
  3270.     mov    bx,word ptr [bp-10]
  3271.     sar    bx,3
  3272.     shl    bx,6
  3273.     add    dx,bx
  3274.     mov    bx,word ptr [bp-12]
  3275.     sar    bx,3
  3276.     shl    bx,1
  3277.     add    dx,bx
  3278.     mov    word ptr [bp-32],ax
  3279.     mov    word ptr [bp-34],dx
  3280.    ;    
  3281.    ;          cachep = & histogram[c0 >> Y_SHIFT][c1 >> C_SHIFT][c2 >> C_SHIFT];
  3282.    ;          /* If we have not seen this color before, find nearest colormap */
  3283.    ;          /* entry and update the cache */
  3284.    ;    
  3285.     les    bx,dword ptr [bp-34]
  3286.     cmp    word ptr es:[bx],0
  3287.     jne    short @13@482
  3288.     ;
  3289.    ;          if (*cachep == 0)
  3290.    ;    
  3291.     mov    ax,word ptr [bp-12]
  3292.     sar    ax,3
  3293.     push    ax
  3294.     mov    ax,word ptr [bp-10]
  3295.     sar    ax,3
  3296.     push    ax
  3297.     mov    ax,di
  3298.     sar    ax,2
  3299.     push    ax
  3300.     push    word ptr [bp+4]
  3301.     call    near ptr fill_inverse_cmap
  3302.     add    sp,8
  3303. @13@482:
  3304.    ;    
  3305.    ;        fill_inverse_cmap(cinfo, c0 >> Y_SHIFT, c1 >> C_SHIFT, c2 >> C_SHIFT);
  3306.    ;          /* Now emit the colormap index for this cell */
  3307.    ;    
  3308.     les    bx,dword ptr [bp-34]
  3309.     mov    ax,word ptr es:[bx]
  3310.     dec    ax
  3311.     mov    word ptr [bp-14],ax
  3312.     ;
  3313.    ;          pixcode = *cachep - 1;
  3314.    ;    
  3315.     les    bx,dword ptr [bp-30]
  3316.     mov    al,byte ptr [bp-14]
  3317.     mov    byte ptr es:[bx],al
  3318.    ;    
  3319.    ;          *outptr = (JSAMPLE) pixcode;
  3320.    ;          /* Compute representation error for this pixel */
  3321.    ;    
  3322.     mov    bx,word ptr DGROUP:my_colormap
  3323.     les    bx,dword ptr [bx]
  3324.     add    bx,word ptr [bp-14]
  3325.     mov    al,byte ptr es:[bx]
  3326.     mov    ah,0
  3327.     sub    di,ax
  3328.    ;    
  3329.    ;          c0 -= (FSERROR) GETJSAMPLE(my_colormap[0][pixcode]);
  3330.    ;    
  3331.     mov    bx,word ptr DGROUP:my_colormap
  3332.     les    bx,dword ptr [bx+4]
  3333.     add    bx,word ptr [bp-14]
  3334.     mov    al,byte ptr es:[bx]
  3335.     mov    ah,0
  3336.     sub    word ptr [bp-10],ax
  3337.    ;    
  3338.    ;          c1 -= (FSERROR) GETJSAMPLE(my_colormap[1][pixcode]);
  3339.    ;    
  3340.     mov    bx,word ptr DGROUP:my_colormap
  3341.     les    bx,dword ptr [bx+8]
  3342.     add    bx,word ptr [bp-14]
  3343.     mov    al,byte ptr es:[bx]
  3344.     mov    ah,0
  3345.     sub    word ptr [bp-12],ax
  3346.    ;    
  3347.    ;          c2 -= (FSERROR) GETJSAMPLE(my_colormap[2][pixcode]);
  3348.    ;          /* Propagate error to adjacent pixels */
  3349.    ;          /* Remember that nextrowerr entries are in reverse order! */
  3350.    ;    
  3351.     mov    ax,di
  3352.     shl    ax,1
  3353.     mov    si,ax
  3354.    ;    
  3355.    ;          val = c0 * 2;
  3356.     ;
  3357.     les    bx,dword ptr [bp-8]
  3358.     mov    word ptr es:[bx-6],di
  3359.    ;    
  3360.    ;          nextrowerr[0-3]  = c0;    /* not +=, since not initialized yet */
  3361.    ;    
  3362.     add    di,si
  3363.    ;    
  3364.    ;          c0 += val;        /* form error * 3 */
  3365.    ;    
  3366.     add    word ptr es:[bx+6],di
  3367.    ;    
  3368.    ;          nextrowerr[0+3] += c0;
  3369.    ;    
  3370.     add    di,si
  3371.    ;    
  3372.    ;          c0 += val;        /* form error * 5 */
  3373.    ;    
  3374.     add    word ptr es:[bx],di
  3375.    ;    
  3376.     ;          nextrowerr[0  ] += c0;
  3377.    ;    
  3378.     add    di,si
  3379.    ;    
  3380.    ;          c0 += val;        /* form error * 7 */
  3381.    ;    
  3382.     les    bx,dword ptr [bp-4]
  3383.     add    word ptr es:[bx+6],di
  3384.    ;    
  3385.    ;          thisrowerr[0+3] += c0;
  3386.    ;    
  3387.     mov    ax,word ptr [bp-10]
  3388.     shl    ax,1
  3389.     mov    si,ax
  3390.    ;    
  3391.    ;          val = c1 * 2;
  3392.    ;    
  3393.     les    bx,dword ptr [bp-8]
  3394.     mov    ax,word ptr [bp-10]
  3395.     mov    word ptr es:[bx-4],ax
  3396.    ;    
  3397.    ;          nextrowerr[1-3]  = c1;    /* not +=, since not initialized yet */
  3398.    ;    
  3399.     add    word ptr [bp-10],si
  3400.     ;
  3401.    ;          c1 += val;        /* form error * 3 */
  3402.    ;    
  3403.     mov    ax,word ptr [bp-10]
  3404.     add    word ptr es:[bx+8],ax
  3405.    ;    
  3406.    ;          nextrowerr[1+3] += c1;
  3407.    ;    
  3408.     add    word ptr [bp-10],si
  3409.    ;    
  3410.    ;          c1 += val;        /* form error * 5 */
  3411.    ;    
  3412.     mov    ax,word ptr [bp-10]
  3413.     add    word ptr es:[bx+2],ax
  3414.    ;    
  3415.    ;          nextrowerr[1  ] += c1;
  3416.    ;    
  3417.     add    word ptr [bp-10],si
  3418.    ;    
  3419.    ;          c1 += val;        /* form error * 7 */
  3420.     ;
  3421.     les    bx,dword ptr [bp-4]
  3422.     mov    ax,word ptr [bp-10]
  3423.     add    word ptr es:[bx+8],ax
  3424.    ;    
  3425.    ;          thisrowerr[1+3] += c1;
  3426.    ;    
  3427.     mov    ax,word ptr [bp-12]
  3428.     shl    ax,1
  3429.     mov    si,ax
  3430.    ;    
  3431.    ;          val = c2 * 2;
  3432.    ;    
  3433.     les    bx,dword ptr [bp-8]
  3434.     mov    ax,word ptr [bp-12]
  3435.     mov    word ptr es:[bx-2],ax
  3436.    ;    
  3437.    ;          nextrowerr[2-3]  = c2;    /* not +=, since not initialized yet */
  3438.    ;    
  3439.     add    word ptr [bp-12],si
  3440.    ;    
  3441.    ;          c2 += val;        /* form error * 3 */
  3442.    ;    
  3443.     mov    ax,word ptr [bp-12]
  3444.     add    word ptr es:[bx+10],ax
  3445.    ;    
  3446.    ;          nextrowerr[2+3] += c2;
  3447.    ;    
  3448.     add    word ptr [bp-12],si
  3449.    ;    
  3450.    ;          c2 += val;        /* form error * 5 */
  3451.    ;    
  3452.     mov    ax,word ptr [bp-12]
  3453.     add    word ptr es:[bx+4],ax
  3454.    ;    
  3455.    ;          nextrowerr[2  ] += c2;
  3456.    ;    
  3457.     add    word ptr [bp-12],si
  3458.    ;    
  3459.    ;          c2 += val;        /* form error * 7 */
  3460.    ;    
  3461.     les    bx,dword ptr [bp-4]
  3462.     mov    ax,word ptr [bp-12]
  3463.     add    word ptr es:[bx+10],ax
  3464.     ;
  3465.    ;          thisrowerr[2+3] += c2;
  3466.    ;          /* Advance to next column */
  3467.    ;    
  3468.     mov    ax,word ptr [bp-36]
  3469.     add    word ptr [bp-18],ax
  3470.    ;    
  3471.    ;          ptr0 += dir;
  3472.    ;    
  3473.     add    word ptr [bp-22],ax
  3474.    ;    
  3475.    ;          ptr1 += dir;
  3476.    ;    
  3477.     add    word ptr [bp-26],ax
  3478.    ;    
  3479.    ;          ptr2 += dir;
  3480.    ;    
  3481.     add    word ptr [bp-30],ax
  3482.    ;    
  3483.    ;          outptr += dir;
  3484.    ;    
  3485.     add    word ptr [bp-4],6
  3486.    ;    
  3487.    ;          thisrowerr += 3;        /* cur-row error ptr advances to right */
  3488.     ;
  3489.     sub    word ptr [bp-8],6
  3490.     sub    word ptr [bp-40],1
  3491.     sbb    word ptr [bp-38],0
  3492. @13@530:
  3493.     cmp    word ptr [bp-38],0
  3494.     jle    @@13
  3495.     jmp    @13@146
  3496. @@13:
  3497.     jne    short @13@602
  3498.     cmp    word ptr [bp-40],0
  3499.     jbe    @@14
  3500.     jmp    @13@146
  3501. @@14:
  3502. @13@602:
  3503.     inc    word ptr [bp-42]
  3504. @13@626:
  3505.     mov    ax,word ptr [bp-42]
  3506.     cmp    ax,word ptr [bp+6]
  3507.     jge    @@15
  3508.     jmp    @13@50
  3509. @@15:
  3510.    ;    
  3511.    ;          nextrowerr -= 3;        /* next-row error ptr advances to left */
  3512.    ;        }
  3513.    ;      }
  3514.    ;      /* Emit converted rows to the output file */
  3515.    ;    
  3516.     lea    ax,word ptr [bp+10]
  3517.     push    ax
  3518.     push    word ptr [bp+6]
  3519.     push    word ptr [bp+4]
  3520.     mov    bx,word ptr [bp+4]
  3521.     mov    bx,word ptr [bx]
  3522.     call    word ptr [bx+62]
  3523.     add    sp,6
  3524.    ;    
  3525.    ;      (*cinfo->methods->put_pixel_rows) (cinfo, num_rows, &output_workspace);
  3526.    ;    
  3527.     pop    di
  3528.     pop    si
  3529.     leave    
  3530.     ret    
  3531. pass2_dither    endp
  3532.     ;
  3533.    ;    METHODDEF void
  3534.    ;    
  3535.     assume    cs:_TEXT
  3536. color_quant_init    proc    near
  3537.     push    bp
  3538.     mov    bp,sp
  3539.     dec    sp
  3540.     dec    sp
  3541.     push    si
  3542.     push    di
  3543.     mov    si,word ptr [bp+4]
  3544.    ;    
  3545.    ;    color_quant_init (decompress_info_ptr cinfo)
  3546.    ;    {
  3547.    ;      int i;
  3548.    ;    
  3549.    ;      /* Lower bound on # of colors ... somewhat arbitrary as long as > 0 */
  3550.    ;    
  3551.     cmp    word ptr [si+24],8
  3552.     jge    short @14@74
  3553.    ;    
  3554.    ;      if (cinfo->desired_number_of_colors < 8)
  3555.    ;    
  3556.     push    offset DGROUP:s@+36
  3557.     mov    bx,word ptr [si+2]
  3558.     call    word ptr [bx]
  3559.     inc    sp
  3560.     inc    sp
  3561. @14@74:
  3562.    ;    
  3563.    ;        ERREXIT(cinfo->emethods, "Cannot request less than 8 quantized colors");
  3564.    ;      /* Make sure colormap indexes can be represented by JSAMPLEs */
  3565.    ;    
  3566.     cmp    word ptr [si+24],256
  3567.     jle    short @14@122
  3568.    ;    
  3569.    ;      if (cinfo->desired_number_of_colors > MAXNUMCOLORS)
  3570.    ;        ERREXIT1(cinfo->emethods, "Cannot request more than %d quantized colors",
  3571.    ;    
  3572.     mov    bx,word ptr [si+2]
  3573.     mov    word ptr [bx+6],256
  3574.     push    offset DGROUP:s@+80
  3575.     mov    bx,word ptr [si+2]
  3576.     call    word ptr [bx]
  3577.     inc    sp
  3578.     inc    sp
  3579. @14@122:
  3580.    ;    
  3581.    ;             MAXNUMCOLORS);
  3582.    ;    
  3583.    ;      /* Allocate and zero the histogram */
  3584.    ;    
  3585.    ;    
  3586.    ;      histogram = (hist3d) (*cinfo->emethods->alloc_small)
  3587.    ;    
  3588.     push    256
  3589.     mov    bx,word ptr [si+2]
  3590.     call    word ptr [bx+22]
  3591.     inc    sp
  3592.     inc    sp
  3593.     mov    word ptr DGROUP:histogram,ax
  3594.    ;    
  3595.    ;                    (HIST_Y_ELEMS * SIZEOF(hist2d));
  3596.     ;
  3597.     xor    di,di
  3598.     jmp    short @14@194
  3599. @14@146:
  3600.    ;    
  3601.    ;      for (i = 0; i < HIST_Y_ELEMS; i++) {
  3602.    ;    
  3603.    ;    
  3604.    ;        histogram[i] = (hist2d) (*cinfo->emethods->alloc_medium)
  3605.    ;    
  3606.     push    2048
  3607.     mov    bx,word ptr [si+2]
  3608.     call    word ptr [bx+26]
  3609.     inc    sp
  3610.     inc    sp
  3611.     mov    bx,di
  3612.     shl    bx,2
  3613.     push    ax
  3614.     push    dx
  3615.     push    bx
  3616.     mov    bx,word ptr DGROUP:histogram
  3617.     pop    ax
  3618.     add    bx,ax
  3619.     pop    ax
  3620.     mov    word ptr [bx+2],ax
  3621.     pop    ax
  3622.     mov    word ptr [bx],ax
  3623.    ;    
  3624.    ;                    (HIST_C_ELEMS*HIST_C_ELEMS * SIZEOF(histcell));
  3625.    ;    
  3626.    ;    
  3627.    ;        jzero_far((void FAR *) histogram[i],
  3628.    ;    
  3629.     push    2048
  3630.     mov    ax,di
  3631.     shl    ax,2
  3632.     mov    bx,word ptr DGROUP:histogram
  3633.     add    bx,ax
  3634.     push    word ptr [bx+2]
  3635.     push    word ptr [bx]
  3636.     call    near ptr _jzero_far
  3637.     add    sp,6
  3638.     inc    di
  3639. @14@194:
  3640.     cmp    di,64
  3641.     jl    short @14@146
  3642.    ;    
  3643.    ;              HIST_C_ELEMS*HIST_C_ELEMS * SIZEOF(histcell));
  3644.    ;      }
  3645.    ;    
  3646.    ;      /* Allocate storage for the internal and external colormaps. */
  3647.    ;      /* We do this now since it is FAR storage and may affect the memory */
  3648.    ;      /* manager's space calculations. */
  3649.    ;    
  3650.    ;    
  3651.    ;      my_colormap = (*cinfo->emethods->alloc_small_sarray)
  3652.    ;                ((long) cinfo->desired_number_of_colors,
  3653.    ;    
  3654.     push    0
  3655.     push    3
  3656.     mov    ax,word ptr [si+24]
  3657.     cwd
  3658.     push    dx
  3659.     push    ax
  3660.     mov    bx,word ptr [si+2]
  3661.     call    word ptr [bx+30]
  3662.     add    sp,8
  3663.     mov    word ptr DGROUP:my_colormap,ax
  3664.     ;
  3665.     ;                 (long) 3);
  3666.     ;
  3667.     ;
  3668.     ;      cinfo->colormap = (*cinfo->emethods->alloc_small_sarray)
  3669.     ;                ((long) cinfo->desired_number_of_colors,
  3670.     ;
  3671.     mov    ax,word ptr [si+139]
  3672.     cwd
  3673.     push    dx
  3674.     push    ax
  3675.     mov    ax,word ptr [si+24]
  3676.     cwd
  3677.     push    dx
  3678.     push    ax
  3679.     mov    bx,word ptr [si+2]
  3680.     call    word ptr [bx+30]
  3681.     add    sp,8
  3682.     mov    word ptr [si+145],ax
  3683.     ;
  3684.     ;                 (long) cinfo->color_out_comps);
  3685.     ;
  3686.     ;      /* Allocate Floyd-Steinberg workspace if necessary */
  3687.     ;      /* This isn't needed until pass 2, but again it is FAR storage. */
  3688.     ;
  3689.     cmp    word ptr [si+22],0
  3690.     je    short @14@266
  3691.     ;
  3692.     ;      if (cinfo->use_dithering) {
  3693.     ;
  3694.     mov    cx,word ptr [si+38]
  3695.     mov    bx,word ptr [si+36]
  3696.     add    bx,2
  3697.     adc    cx,0
  3698.     xor    dx,dx
  3699.     mov    ax,6
  3700.     call    near ptr N_LXMUL@
  3701.     mov    word ptr [bp-2],ax
  3702.     ;
  3703.     ;        size_t arraysize = (size_t) ((cinfo->image_width + 2L) * 3L * SIZEOF(FSERROR));
  3704.     ;
  3705.     ;
  3706.     push    word ptr [bp-2]
  3707.     mov    bx,word ptr [si+2]
  3708.     call    word ptr [bx+26]
  3709.     inc    sp
  3710.     inc    sp
  3711.     mov    word ptr DGROUP:evenrowerrs+2,dx
  3712.     mov    word ptr DGROUP:evenrowerrs,ax
  3713.    ;    
  3714.    ;        evenrowerrs = (FSERRPTR) (*cinfo->emethods->alloc_medium) (arraysize);
  3715.    ;    
  3716.     push    word ptr [bp-2]
  3717.     mov    bx,word ptr [si+2]
  3718.     call    word ptr [bx+26]
  3719.     inc    sp
  3720.     inc    sp
  3721.     mov    word ptr DGROUP:oddrowerrs+2,dx
  3722.     mov    word ptr DGROUP:oddrowerrs,ax
  3723.    ;    
  3724.    ;        oddrowerrs  = (FSERRPTR) (*cinfo->emethods->alloc_medium) (arraysize);
  3725.    ;        /* we only need to zero the forward contribution for current row. */
  3726.    ;    
  3727.     push    word ptr [bp-2]
  3728.     push    word ptr DGROUP:evenrowerrs+2
  3729.     push    word ptr DGROUP:evenrowerrs
  3730.     call    near ptr _jzero_far
  3731.     add    sp,6
  3732.    ;    
  3733.    ;        jzero_far((void FAR *) evenrowerrs, arraysize);
  3734.    ;    
  3735.     mov    word ptr DGROUP:on_odd_row,0
  3736. @14@266:
  3737.    ;    
  3738.    ;        on_odd_row = FALSE;
  3739.    ;      }
  3740.    ;    
  3741.    ;      /* Indicate number of passes needed, excluding the prescan pass. */
  3742.    ;    
  3743.     inc    word ptr [si+147]
  3744.    ;    
  3745.     ;      cinfo->total_passes++;    /* I always use one pass */
  3746.    ;    
  3747.     pop    di
  3748.     pop    si
  3749.     leave    
  3750.     ret    
  3751. color_quant_init    endp
  3752.    ;    
  3753.    ;    METHODDEF void
  3754.    ;    
  3755.     assume    cs:_TEXT
  3756. color_quant_doit    proc    near
  3757.     push    bp
  3758.     mov    bp,sp
  3759.     push    si
  3760.     push    di
  3761.     mov    si,word ptr [bp+4]
  3762.    ;    
  3763.    ;    color_quant_doit (decompress_info_ptr cinfo, quantize_caller_ptr source_method)
  3764.    ;    {
  3765.    ;      int i;
  3766.    ;    
  3767.    ;      /* Select the representative colors */
  3768.    ;    
  3769.     push    si
  3770.     call    near ptr select_colors
  3771.     inc    sp
  3772.     inc    sp
  3773.    ;    
  3774.    ;      select_colors(cinfo);
  3775.    ;      /* Pass the external colormap to the output module. */
  3776.    ;      /* NB: the output module may continue to use the colormap until shutdown. */
  3777.    ;    
  3778.    ;    
  3779.    ;      (*cinfo->methods->put_color_map) (cinfo, cinfo->actual_number_of_colors,
  3780.    ;    
  3781.     push    word ptr [si+145]
  3782.     push    word ptr [si+143]
  3783.     push    si
  3784.     mov    bx,word ptr [si]
  3785.     call    word ptr [bx+60]
  3786.     add    sp,6
  3787.    ;    
  3788.    ;                        cinfo->colormap);
  3789.     ;      /* Re-zero the histogram so pass 2 can use it as nearest-color cache */
  3790.     ;
  3791.     xor    di,di
  3792.     jmp    short @15@98
  3793. @15@50:
  3794.    ;    
  3795.    ;      for (i = 0; i < HIST_Y_ELEMS; i++) {
  3796.    ;    
  3797.    ;    
  3798.    ;        jzero_far((void FAR *) histogram[i],
  3799.    ;    
  3800.     push    2048
  3801.     mov    ax,di
  3802.     shl    ax,2
  3803.     mov    bx,word ptr DGROUP:histogram
  3804.     add    bx,ax
  3805.     push    word ptr [bx+2]
  3806.     push    word ptr [bx]
  3807.     call    near ptr _jzero_far
  3808.     add    sp,6
  3809.     inc    di
  3810. @15@98:
  3811.     cmp    di,64
  3812.     jl    short @15@50
  3813.    ;    
  3814.    ;              HIST_C_ELEMS*HIST_C_ELEMS * SIZEOF(histcell));
  3815.    ;      }
  3816.     ;      /* Perform pass 2 */
  3817.    ;    
  3818.     cmp    word ptr [si+22],0
  3819.     je    short @15@170
  3820.    ;    
  3821.    ;      if (cinfo->use_dithering)
  3822.    ;    
  3823.     push    offset pass2_dither
  3824.     jmp    short @15@194
  3825. @15@170:
  3826.    ;    
  3827.    ;        (*source_method) (cinfo, pass2_dither);
  3828.    ;      else
  3829.    ;    
  3830.     push    offset pass2_nodither
  3831. @15@194:
  3832.     push    si
  3833.     call    word ptr [bp+6]
  3834.     add    sp,4
  3835.    ;    
  3836.    ;        (*source_method) (cinfo, pass2_nodither);
  3837.    ;    
  3838.     pop    di
  3839.     pop    si
  3840.     pop    bp
  3841.     ret    
  3842. color_quant_doit    endp
  3843.    ;    
  3844.    ;    METHODDEF void
  3845.    ;    
  3846.     assume    cs:_TEXT
  3847. color_quant_term    proc    near
  3848.     push    bp
  3849.     mov    bp,sp
  3850.    ;    
  3851.    ;    color_quant_term (decompress_info_ptr cinfo)
  3852.    ;    {
  3853.    ;      /* no work (we let free_all release the histogram/cache and colormaps) */
  3854.    ;      /* Note that we *mustn't* free the external colormap before free_all, */
  3855.    ;      /* since output module may use it! */
  3856.    ;    
  3857.     pop    bp
  3858.     ret    
  3859. color_quant_term    endp
  3860.     ;
  3861.    ;    METHODDEF void
  3862.    ;    
  3863.     assume    cs:_TEXT
  3864. color_quantize    proc    near
  3865.     push    bp
  3866.     mov    bp,sp
  3867.     push    si
  3868.     mov    si,word ptr [bp+4]
  3869.    ;    
  3870.    ;    color_quantize (decompress_info_ptr cinfo, int num_rows,
  3871.    ;            JSAMPIMAGE input_data, JSAMPARRAY output_data)
  3872.    ;    {
  3873.    ;    
  3874.     push    offset DGROUP:s@+125
  3875.     mov    bx,word ptr [si+2]
  3876.     call    word ptr [bx]
  3877.     inc    sp
  3878.     inc    sp
  3879.    ;    
  3880.    ;      ERREXIT(cinfo->emethods, "Should not get here!");
  3881.    ;    
  3882.     pop    si
  3883.     pop    bp
  3884.     ret    
  3885. color_quantize    endp
  3886.    ;    
  3887.    ;    GLOBAL void
  3888.    ;    
  3889.     assume    cs:_TEXT
  3890. _jsel2quantize    proc    near
  3891.     push    bp
  3892.     mov    bp,sp
  3893.     push    si
  3894.     mov    si,word ptr [bp+4]
  3895.    ;    
  3896.    ;    jsel2quantize (decompress_info_ptr cinfo)
  3897.    ;    {
  3898.    ;    
  3899.     cmp    word ptr [si+20],0
  3900.     je    short @18@146
  3901.    ;    
  3902.    ;      if (cinfo->two_pass_quantize) {
  3903.    ;        /* Make sure jdmaster didn't give me a case I can't handle */
  3904.     ;
  3905.     cmp    word ptr [si+53],3
  3906.     jne    short @18@98
  3907.     cmp    word ptr [si+46],3
  3908.     je    short @18@122
  3909. @18@98:
  3910.    ;    
  3911.    ;        if (cinfo->num_components != 3 || cinfo->jpeg_color_space != CS_YCbCr)
  3912.    ;    
  3913.     push    offset DGROUP:s@+146
  3914.     mov    bx,word ptr [si+2]
  3915.     call    word ptr [bx]
  3916.     inc    sp
  3917.     inc    sp
  3918. @18@122:
  3919.    ;    
  3920.    ;          ERREXIT(cinfo->emethods, "2-pass quantization only handles YCbCr input");
  3921.     ;
  3922.     mov    bx,word ptr [si]
  3923.     mov    word ptr [bx+48],offset color_quant_init
  3924.    ;    
  3925.    ;        cinfo->methods->color_quant_init = color_quant_init;
  3926.    ;    
  3927.     mov    bx,word ptr [si]
  3928.     mov    word ptr [bx+52],offset color_quant_prescan
  3929.    ;    
  3930.    ;        cinfo->methods->color_quant_prescan = color_quant_prescan;
  3931.    ;    
  3932.     mov    bx,word ptr [si]
  3933.     mov    word ptr [bx+54],offset color_quant_doit
  3934.    ;    
  3935.    ;        cinfo->methods->color_quant_doit = color_quant_doit;
  3936.    ;    
  3937.     mov    bx,word ptr [si]
  3938.     mov    word ptr [bx+56],offset color_quant_term
  3939.    ;    
  3940.    ;        cinfo->methods->color_quant_term = color_quant_term;
  3941.    ;    
  3942.     mov    bx,word ptr [si]
  3943.     mov    word ptr [bx+50],offset color_quantize
  3944. @18@146:
  3945.    ;    
  3946.    ;        cinfo->methods->color_quantize = color_quantize;
  3947.    ;      }
  3948.     ;
  3949.     pop    si
  3950.     pop    bp
  3951.     ret    
  3952. _jsel2quantize    endp
  3953. _TEXT    ends
  3954. _BSS    segment word public 'BSS'
  3955. on_odd_row    label    word
  3956.     db    2 dup (?)
  3957. evenrowerrs    label    dword
  3958.     db    4 dup (?)
  3959. boxlist    label    word
  3960.     db    2 dup (?)
  3961. oddrowerrs    label    dword
  3962.     db    4 dup (?)
  3963. numboxes    label    word
  3964.     db    2 dup (?)
  3965. histogram    label    word
  3966.     db    2 dup (?)
  3967. my_colormap    label    word
  3968.     db    2 dup (?)
  3969. _BSS    ends
  3970. _DATA    segment word public 'DATA'
  3971. s@    label    byte
  3972.     db    'Selected %d colors for quantization'
  3973.     db    0
  3974.     db    'Cannot request less than 8 quantized colors'
  3975.     db    0
  3976.     db    'Cannot request more than %d quantized colors'
  3977.     db    0
  3978.     db    'Should not get here!'
  3979.     db    0
  3980.     db    '2-pass quantization only handles YCbCr input'
  3981.     db    0
  3982. _DATA    ends
  3983. _TEXT    segment byte public 'CODE'
  3984. _TEXT    ends
  3985.     extrn    _jzero_far:near
  3986. _pass2_dither    equ    pass2_dither
  3987. _median_cut    equ    median_cut
  3988. _my_colormap    equ    my_colormap
  3989. _color_quant_prescan    equ    color_quant_prescan
  3990. _color_quant_term    equ    color_quant_term
  3991. _color_quantize    equ    color_quantize
  3992. _histogram    equ    histogram
  3993. _color_quant_doit    equ    color_quant_doit
  3994. _color_quant_init    equ    color_quant_init
  3995. _numboxes    equ    numboxes
  3996. _oddrowerrs    equ    oddrowerrs
  3997.     public    _jsel2quantize
  3998. _update_box    equ    update_box
  3999. _find_biggest_color_pop    equ    find_biggest_color_pop
  4000. _compute_color    equ    compute_color
  4001. _find_biggest_volume    equ    find_biggest_volume
  4002. _find_nearby_colors    equ    find_nearby_colors
  4003. _find_best_colors    equ    find_best_colors
  4004.     extrn    N_LDIV@:far
  4005. _boxlist    equ    boxlist
  4006. _evenrowerrs    equ    evenrowerrs
  4007. _fill_inverse_cmap    equ    fill_inverse_cmap
  4008.     extrn    N_LXMUL@:far
  4009.     extrn    N_LXLSH@:far
  4010. _on_odd_row    equ    on_odd_row
  4011. _select_colors    equ    select_colors
  4012. _pass2_nodither    equ    pass2_nodither
  4013. _remap_colormap    equ    remap_colormap
  4014.     end
  4015.